Shell programming input and output, control structure, function

Source: Internet
Author: User

Shell programming input and output and control structure

I. shell Input and Output

1, echo

[Root @ test3 tmp] # vim a. sh #! /Bin/bashecho "sss \ n" is output, but "echo {sss \ n} is not output, however, the \ echo-n "sss \ n" output does not contain line breaks. The default line breaks echo-e "sss \ n" to explain the escape characters, echo nihao dajia output echo ccc * output ccc and the file [root @ test3 tmp] # chmod + x. sh [root @ test3 tmp] #. /. shsss \ n {sssn} sss \ nsssnihao dajiaccc. sh io. sh keyring-KY2TBC keyring-p0MelO memcached. pid mysql. sock orbit-TPD orbit-root pear pulse-0uxLzihOzSmN pulse-1NOdSg3wjC4T sess_fuka99ealkkr93ppoafkupknu4geag5l platinum sh-thd-1378728692 virtual-root.8TPViI virtual-root.9HGFqQ virtual-root.Dx7gP2 virtual-root.izuZCV virtual-root.okJZiV virtual-root.rtOlcM virtual-root.tIZkT5 VMwareDnD vmware-root virtual-root.Ulx4o4 xdg-screensaver-root -- 0.0

2, read

Even

If read only specifies one variable, all input will be paid to the variable until the carriage return or file Terminator is encountered. If read specifies multiple variables, the input will be paid to the variables in order, and space will be used as the separator for these variables [root @ test3 tmp] # vim B. sh #! /Bin/bashread param1read param2read param3 param4 echo "first: $ param1" echo "second: $ param2" echo "third: $ param3" echo "forth: $ param4 "[root @ test3 tmp] # chmod + x B. sh [root @ test3 tmp] #. /B. enter shfirstsecondthird here and press enter to show that param4 does not accept the parameter first: firstsecond: secondthird: thirdforth: [root @ test3 tmp] # [root @ test3 tmp] #. /B. shaabb cc ddee at this time, param4 does not accept the parameter first: aasecond: bb cc ddthird: eeforth: [root @ test3 tmp] #. /B. shaabb cc ddee ff at this time, param receives the first: aasecond: bb cc ddthird: eeforth: ff parameter.


3. MPS queue

Print the received message to the local file c.txt [root @ test3 tmp] # df-h | awk "{print $1}" Double quotation marks are incorrect Filesystem Size Used Avail Use % Mounted on/dev/sda2 19G 4.1G 14G 24%/tmpfs 495 M 276 K 495 M 1%/dev/shm/dev/sda1 291 M 30 M 246 M 11%/boot [root @ test3 tmp] # df-h | awk '{print $1}' is the pair of Filesystem/dev/sda2tmpfs/dev/sda1 [root @ test3 tmp] # df-h | awk '{print $1 }' | grep-v Filesystem/dev/sda2tmpfs/dev/sda1 [root @ test3 tmp] # df-h | awk '{print $1}' | grep-v Filesystem | tee c.txt tee is to input the standard output to the file/dev/sda2tmpfs/dev/sda1 [root @ test3 tmp] # cat c.txt/dev/sda2tmpfs/dev/sda1 [root @ test3 tmp] # df-h | awk '{print $1}' | grep-v Filesystem | tee-a c.txt tee-a is used to append the standard output to the/dev/sda2 tmpfs/dev file. /sda1 [root @ test3 tmp] # cat c.txt/dev/sda2tmpfs/dev/sda1/dev/sda2tmpfs/dev/sda1


4. Redirection

[Root @ test3 tmp] # vim d.txt [root @ test3 tmp] # input command of cat d.txt nihaoma screen to catch up with d.txt. When EOF is encountered, stop [root @ test3 tmp] # cat> d.txt <EOF> this is test> I l u> EOF [root @ test3 tmp] # cat d.txt nihaomathis is testi l u [root @ test3 tmp] # cat> d.txt <EOF> $ LOGNAME environment variable explained> EOF [root @ test3 tmp] # cat d.txt nihaomathis is testi l uroot hosts [root @ test3 tmp] # sort <d.txt> e.txt [root @ test3 tmp] # cat e.txt I l unihaomarootthis is test


Ii. Control Structure

1. if statement

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0551005522-0.jpg "title =" NTVKB $ 78PE ~ C (Q ~ N1_kyqkak.jpg "alt =" 170138897.jpg"/>

Shell test usage 1) determine the expression if test (the expression is true) if test! Expression is false test expression 1-a expression 2 both are true test expression 1-o expression 2 two are true 2) judge the length of the test-n string. The length of the non-zero test-z string is zero. test string 1 = string 2 string is equal to test string 1! = String 2 string not equal to 3) judge integer test integer 1-eq integer 2 integer equal test integer 1-ge integer 2 integer 1 greater than or equal to integer 2 test integer 1-gt integer 2 integer 1 greater than integer 2 test integer 1- le integer 2 integer 1 less than or equal to integer 2 test integer 1-lt integer 2 integer 1 less than integer 2 test integer 1-ne integer 2 integer 1 not equal to integer 24) determine whether the file test File1-ef File2 has the same device number and I node number test File1-nt File2. File 1 is older than file 2's new test File1-ot File2 file 1 and file 2. the test-B File exists, and the test-c File of the block device exists, and the test-d File of the character device exists, and the test-e File of the test-e directory exists. the File exists and the test-g File is a regular File, and the IDtest-G File is set to exist, and the IDtest-h File in the valid group exists and is a symbolic link -L) the test-k File exists and the sticky bit test-B File is set to exist. The test-L File is a block device File and the File is a symbolic link with-h) the test-o File exists and is a valid user's IDtest-p File. a named pipe test-r File exists and the readable test-s File exists and is a socket named test -t FD File descriptor is a test-u File opened on a terminal that exists and its set-user-id test-w File is set to exist and can be written to test-x File. the file exists and is executable.

System Logon

[Root @ test3 tmp] # vim a. sh [root @ test3 tmp] #./a. shplease input username: rootplease input password: 123 #! /Bin/bashecho-n "please input username:" read usernameecho-n "please input password:" read passwordif [$ username! = "Root"] [] must contain a space between then echo "your username error" elif [$ password! = "123"] then echo "your password error" else echo "login successful" fi

Cp command

[Root @ test3 tmp] # cat B. sh #! /Bin/basffe cp my. file my. file. back to determine whether the command is successful then echo "successful" else echo "copy file fail"> & 2fi [root @ test3 tmp] #. /B. shcp: cannot stat 'my. file': No such file or directory copy file fail


2. case statement

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/055100A21-1.jpg "title =" % NF6TJ4 ~ Wc100000000jcumv000000002zq.jpg "alt =" 182541384.jpg"/>


[root@test3 tmp]# ./c.shEnter a number from 1 to 3:3you select 3[root@test3 tmp]# cat c.sh#!/bin/bashecho -n "Enter a number from 1 to 3:"read ANScase $ANS in1)  echo "you select 1"  ;;2)  echo "you select 2"  ;;3)  echo "you select 3"  ;;*)  echo "error" >&2  exit  ;;esac


3. for statement

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/055100K04-2.jpg "title =" 4 '_ C ~ O3ho0tu4?o=12dl99t.jpg "alt =" 183318307.jpg"/>

[Root @ test3 tmp] # chmod + x e. sh [root @ test3 tmp] #./e. sh123456 [root @ test3 tmp] # cat e. sh #! /Bin/bashfor loop in 1 2 3 4 5 6do echo-n "$ loop" done [root @ test3 tmp] # cat f. sh #! /Bin/bashfor ss in red black orage here red black orage is input to ssdo echo "$ ss" donefor ww in "red black orage" here red black orage is used as give wwdo echo "$ ww" done [root @ test3 tmp] # chmod + x f. sh [root @ test3 tmp] #. /f. shredblackoragered black orage uses the file content as the for value, and the characters in the file are separated by spaces or carriage return [root @ test3 tmp] # cat g. sh #! /Bin/bashfor file in 'cat/tmp/dd.txt 'do echo $ filedone [root @ test3 tmp] #. /g. shparam1param2param3param4param5 [root @ test3 tmp] # cat dd.txt param1 param2 param3param4param5


4. until statement

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0551004428-3.jpg "title =" m9p@ggf7kmg@p2w?)14h=9s.jpg "alt =" 185235220.jpg"/>

Monitoring Disk

Monitors disk usage. If it is greater than 10%, an email is sent to the root user [root @ test3 tmp] # vim a. sh #! /Bin/bashsda1 = 'df | grep sda1 | awk '{print $5}' | sed's/% // g'' sda2 = 'df | grep sda2 | awk' {print $5} '| sed's/% // g'' until [$ sda1-gt "10"] | [$ sda2-gt "10"] dosda1 = 'df | grep sda1 | awk '{print $5}' | sed's/% // g'' sda2 = 'df | grep sda2 | awk '{print $5} '| sed's/% // g'' sleep 60 echo "your filesystem is full" | mail rootdone


5. while statement

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/05510031O-4.jpg "title =" kk7le409y6@7ag83r9a%hbo.jpg "alt =" 191535609.jpg"/>

If the command is false, stop the loop or use break to exit the loop.

[Root @ test3 tmp] # cat B. sh #! /Bin/bashwhile read linedo echo $ lineechodone <bb.txt uses the content of a file as the while input [root @ test3 tmp] # cat bb.txt redblackoragewhite [root @ test3 tmp] #. /B. shredblackoragewhite


Iii. Functions

The return value that a function cannot return cannot be assigned to a variable.

[Root @ test3 tmp] # cat fun. sh #! /Bin/bashfunction hello () {echo "HELLO, WELCOME, today is 'date + % Y-% m-% d' "echo" number of parameters is $ # "number of input parameters echo" name is $1 "first parameter echo" sex is $2 "second parameter echo" country is $3 "third parameter echo" string is $ * "set of all parameters if [$? = 0] 0 indicates that the program successfully executes then echo "status of function is good" else echo "error" fi} echo "now going function" hello chen man china calls function echo" function end "[root @ test3 tmp] #. /fun. shnow going functionHELLO, WELCOME, today is 2013-09-15number of parameters is 3 name is chensex is mancountry is chinastring is chen man chinastatus of function is goodfunction end



This article is from the "alive" blog. For more information, contact the author!

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.