Shell script programming string testing and looping

Source: Internet
Author: User

== : Two strings are equal

! =: Two characters not equal

> : Whether a string is greater than another string

< : Whether a string is less than another string

- N String : Tests whether the specified string is empty

- s string : Tests whether the specified string is not empty

For example

Determine if the user name is the same as the group name

#!/bin/bash

if! ID $ &>/dev/null; them

echo "Nouser"

Exit

Fi

If [' id–n–u ' = = ' id–n–u $ ']; Then

echo "OK"

Else

echo "Notok"

Fi

Write a script

pass a parameter to the script, if the argument is Q or Q or quit exit the script, or the user's parameters will be displayed

#!/bin/bash

# determine if a script exists

if [$ = ' Q ']; Then

echo "Exit"

Exit

elif [$ = ' Q ']; Then

echo "Exit"

Exit

elif [$ = ' quit ']; Then

echo "Exit"

Exit

elif [$ = ' Quit ']; Then

echo "Exit"

Exit

Elsee

echo $

Fi

Practice

Pass 3 parameters to the script, the first is an integer, the second is an arithmetic operator, and the third is the result of the calculation is displayed to preserve 2 for precision, shaped like ./CALC.SH5/2

tip: Calculate using BC command, in BC used in Scale the precision can be preserved, using echo "scale=2 ; 111/22 ; "| BC, keep it in a variable, directly referencing

Practice

write a script that determines the current main sentence CPU producers, whose information is /proc/cpuinfo in the file Evderid in a row

if the manufacturer AUTHENTICAMD is displayed as AMD Company, if the manufacturer is Genuineintel is displayed as Intel Company, otherwise it will show up as a non-mainstream company

Practice

write a script that passes to the script. 3 integers, interpreting the maximum and minimum numbers, and displaying the

you can set a variable max=0 , and then let MAX with the $ to compare , if greater than MAX it will $ value is assigned to the MAX , then let MAX with the $ to make comparisons, one analogy

Practice

Pass 3 parameters to the script parameters are user names, the user's account confidence is extracted to place in /tmp/testusers.txt file, and requires a line number at the beginning of each line

Loop: Specify entry condition Exit condition

for Loops

for variables inch List ( The number in the variable goes through the number in the list and exits )

Do

Loop body

Done

seq : [ number of steps to start in ] end number, this is an order to pass `` referencing command results

The variable is the default string at the beginning.

declare–i sum=0 : Defined as integral type

- x : defined as environment variable

#!/bin/bash

Declare–i sum=0

For I in (1..100)

Do

letsum=$[$SUM + $I];

Done

echo "The sum is: $SUM"

Practice

Say hello to every user in the system, that is, traverse each user

Use wc–l/etc/passwd

Lines= ' wc–l/etc/passwd | Cut–d ' –f1 '

For I in ' seq 1 $LINES '

Do

echo "Hell ' head–n $I/etc/passwd | Tail-1 | Cut–d:-f1 ' "

Done

Write a script

Set Variable FIFE the value is /etc/passwd , once want to /etc/passwd say hello to each user and show the Shell , shaped like Helloyourshell : /bin/bash , Count the total number of users, and point to the default user greeting

Write a script

Add Ten a user User1 to the User10 , the password is the same as the user name, but only if the user name does not exist can be added, extended: accept a parameter Add : Add User User1...user10 , del : Delete User User1...user10 , the other exits

Write a script

Calculation - all that can be 3 The and of positive integers that are evenly divisible ( modulus, residual % 3%2=1)

Write a script

Calculation - within the sum of all the odd and all the even and, respectively displayed

write a script that shows all the defaults on the current system, respectively Shell to be Bash the user and default Shell to be /sbin/nologin users, and statistics of various Shell The total number of users below displays the resulting shape as

BASH , 3users , they is :

Root , Redhat , Gentoo

Nologin , 2users , they is :

bin , FTP


This article is from the Linux root file system blog, so be sure to keep this source http://amazonjian.blog.51cto.com/9604681/1709966

Shell script programming string testing and looping

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.