The most complete shell script statement syntax (super detailed)

Blog post outline:

  • 1. Talking script-read statement
  • 2. Test characters in shell script
  • < li>3. Examples of judgment scripts

  • 4. Conditional judgment-if statement
  • 5. Conditional judgment-case statement
  • 6. Conditional judgment- Function function combined with case statement
  • 7. Loop judgment-while, until statement
  • 8. Fixed loop-for…do…done statement
  • < li>9. Loop-cut and set combined with for statement

  • 10. Examples of other script types
  • 11. Shell script tracking and debugging

< p>The more standardized the writing of the shell script, the better, so usually the first few lines of each shell script will have the following comments related to the following aspects:

  • Shell used by the script , Such as /bin/bash;
  • The version information of the script;
  • The author and contact information of the script;
  • The history of the script (record of writing time);< /li>
  • The more special commands in the script are issued by means of absolute paths;
  • The environment variables required for script operation are pre-declared and set.

Don’t talk nonsense, just use the grammar example:

1. Dialogue script-read statement

Requirement①:
first name and 2. last name, and finally displayed on the screen: “Your full name is:” content:

[[emailprotected] ~]# vim 1.sh #Edit the script, the content is as follows

#!/bin/bash
echo -e "yong lai xian shi wen jian full name: "
read -p "qing shu ru fir filename:" firname
read -p "qing shu ru sec filename:" secname
echo -e " your full name is ${firname}${secname} ."
#The " " after echo means line break
[[emailprotected] ~]# sh 1.sh #Execute script
yong lai xian shi wen jian full name:< br />
qing shu ru fir filename:lv #Manually enter the beginning of the file name
qing shu ru sec filename:jian #Manually enter the end of the file name

your full name is lvjian #It will automatically combine the beginning and end and output it

Requirements②:
Assuming I want to create three empty files (via touch), the filename is the most The beginning is determined by the current user’s input. Assuming that the user enters the filename, today’s date is 2019/08/25. I want the date of the previous day, yesterday, and today to create these files.

[[email Protected] ~]# vim 2.sh #Edit script

#!/bin/bash
echo -e "yi ci chuang jian san ge file. "
read -p "qing shu ru filename:" filename
filename=${filename:-file}
date1=$(date --date '1 days ago' +%Y%m%d)
date2=$(date --date '2 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1="${filename}${date1}"
file2="${filename}${date2}"
file3="${filename}${date3}"
touch "${file1}"
touch "${file2}"
touch "${file3}"
[[emailprotected] ~]# sh 2.sh #Execute script
yi ci chuang jian san ge file.

qing shu ru filename:lv #Enter the beginning of a custom file name

[[emailprotected] ~]# find /root -name "lv*" #Check if the creation is successful
/root/lv20190825
/root/lv20190827
/root/lv20190826

Requirements② :
If we want the user to input two variables, then multiply the contents of the two variables, and finally output the result of the multiplication.

[[email Protected] ~]# vim 3.sh #Edit script

#!/bin/bash
echo -e " zhe shi yi ge suan cheng fa de jiao ben: "
read -p "qing shu ru yi ge shu zi:" A
read -p "qing shu ru di er ge shu zi:" B
sum=`echo "scale=4; ${A} * ${B}" | bc`
echo -e " ${A}x${B} ==> ${sum}."
[[email Protected] ~]# sh 3.sh #Execute script

zhe shi yi ge suan cheng fa de jiao ben:

qing shu ru yi ge shu zi:3 #Enter the first number
qing shu ru di er ge shu zi:4 #Enter the second number

3x4 ==> 12. #Output the result< /pre>

2, the test characters in the shell script

The most complete shell script syntax usage (super detailed)

The most complete shell script syntax usage (super detailed)

The most complete shell script syntax usage (super detailed) The most complete shell script syntax usage (super detailed)

All the above tests are carried out through test. You can use "[ ]" instead. Write the type to be tested and the specified name in "[ ]", but there must be spaces on both sides of the brackets. (It is recommended to use brackets "[ ]")

3. Examples of judgment scripts

Requirements①:

  1. Whether this file exists, if it does not exist, give a "Filename does not exist" message, and interrupt the program;
  2. If this file exists, it will be judged as a file or directory, and the result will be output " Filename is regular file" or "Filename is directory"
  3. Judging the authority that the executor has on this file or directory, and output the authority data!
[[email protected] ~]# vim 4.sh #edit script

#!/bin/bash
echo "yong lai ce shi wen jian huo dirctory."
read -p "qing shu ru yi ge wen jian ming:" filename
test -z ${filename} && echo -e " qing shu ru yi ge filename." && exit 0
test! -e ${filename} && echo "filename does not exitst." && exit 0
test -f ${filename} && filetype="file"
test- d ${filename} && filetype="directory"
test -r ${filename} && prem="read"
test -w ${filename} && prem="${prem}+write"
test -x ${filename} && prem="${prem}+exe"
echo -e " this is a ${filetype},it's perm.. is ${prem} ."
[[email Protected] ~]# sh 4.sh #Execute script
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/root # Enter a directory name

this is a directory,it's perm.. is read+write+exe. #Results output after script execution
[[emailprotected] ~]# sh 4.sh #Execute the script again
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/etc/passwd #Input a file

this is a file,it's perm.. is read+write. #Results output after script execution

Requirements②:

1. When a program is executed, the program will let the user enter Y or N.
2, if the user enters Y or y, it will display OK, continue.
3, if the user enters N or n, it will display ON, interrupt.
4, if it is not Y /y/N/n, then the script will loop endlessly until it is manually exited or the correct value is entered (in fact, with a slight change, it can be changed to enter "Y" if you press Enter by default, Do your own research).

[[email Protected] ~]# vim 5.sh #Edit script

#!/bin/bash
while ["${yn}" != "Y" -o "${yn}" != "y" -o "${yn}" != "N" -o "${yn}" != "n" ]
do
read -p "qing shu ru'Y' or'N':" yn
[ "${yn}" == "Y" -o "${yn}" == "y" -o "${yn}" == ""] && echo -e " OK,continue." && exit 0
[ "${yn}" == "N" -o "${yn}" = = "n"] && echo -e " ON,interrupt." && exit 0
done
[[emailprotected] ~]# sh 5.sh #The script is executed multiple times to test whether Meet demand
qing shu ru'Y' or'N':

OK,continue.
[[emailprotected] ~]# sh 5.sh
qing shu ru'Y' or'N':y

OK,continue.
[[emailprotected] ~]# sh 5.sh
qing shu ru'Y' or 'N':n

ON,interrupt.
[[email protected] ~]# sh 5.sh
qing shu ru'Y' or'N':u< br />qing shu ru'Y' or'N':i
qing shu ru'Y' or'N':N

ON,interrupt.

Requirements③:
1. What is the file name of the program?
2. How many parameters are there?
3. If the number of parameters is less than 2, it will inform the user that the number of parameters is too small.
4. What is the content of all the parameters?
5. What is the first parameter?
6. What is the second parameter?

[[emailprotected] ~]# vim 6.sh #Edit the script as follows

#!/bin/bash
echo -e " cheng xu de wen jian ming shi ${0}"
echo -e " yi gong you $# ge can shu."
[ $# -lt 2] && echo "can shu tai shao le ." && exit 0
echo "your whole parameter is ==>'$*'."
echo "the 1st parameter ${1}."
echo "the 2nd parameter ${2}."
[[emailprotected] ~]# sh 6.sh abc #Execute script

cheng xu de wen jian ming shi 6.sh

yi gong you 3 ge can shu.
your whole parameter is ==>'abc'.
the 1st parameter a.
the 2nd parameter b.
[[email Protected] ~]# sh 6.sh a #Execute the script again

cheng xu de wen jian ming shi 6.sh

yi gong you 1 ge can shu.
can shu tai shao le .
#In order not to embarrass myself, I used pinyin above, so be considerate of [見面].

Requirement ④:
Check whether the www / ftp / mail service is enabled on this machine, and display the results intuitively

[[ email protected] ~]# vim 11.sh 

#!/bin/bash
file="/dev/shm/a.txt"
netstat -anpt> ${ file}
awk -F:'{print $4}' ${file} | awk'{print $1}' | grep "80" &> /dev/null
if [$? -eq 0 ]
then
echo -e "www service is up "
fi
awk'{print $4}' ${file} | egrep "20|21" &> /dev/null
if [$? -eq 0 ]
then
echo -e "ftp service is up "
fi
awk'{print $4 }'${file} | grep "25" &> /dev/null
if [$? -eq 0 ]
then
echo -e "mail service is up "< br />fi
[[emailprotected] ~]# sh 11.sh #Execute script test
mail service is up

[[emailprotected] ~]# systemctl start httpd #Start www service and test again
[[emailprotected] ~]# sh 11.sh
www service is up

mail service is up

Requirement ⑤:
Everyone knows that the first paragraph after the script is $1, and the second paragraph is $2.... Then can it be offset, assuming that the original $2 becomes $1.

[[email protected] ~]# vim 7.sh #Edit the script as follows

#!/bin/bash
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift 3
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
#" The default “shift” parameter above is to shift by 1 position, you can also specify the shift parameter, such as “shift 3” means to shift backward by three
[[emailprotected] ~]# sh 7.sh abc #Execute the script and append three parameters
total parameter number is ==> 3
your whole parameter is ==> abc
total parameter number is ==> 2
your whole parameter is ==> bc
total parameter number is ==> 2
your whole parameter is ==> bc
#From the output result, it can be found that the offset is cumulative. The default is 1 bit at a time,
#The second time is shifted by 3 bits, then the original parameter has actually been shifted by 4 bits (because of accumulation)
#But there are only three parameters, so It will cyclically shift, so the result is still b and c.

For the explanation of "$#" and "$*" in the above script, please refer to the following explanation:
The most complete shell script syntax usage (super detailed)

4. Condition judgment-if statement

Requirement①:

1. When a program is executed, the program will let the user enter Y or N.
2. If the user enters Y or y or directly press the enter key, it will display OK, continue.
3, If the user enters N or n, it will display ON, interrupt.< br>4. If it is not a character within Y/y/N/n, it will output "I don't know what your choice is"

[[emailprotected] ~]# vim 11. sh #Write script

#!/bin/bash
read -p "Please input (Y/N): "yn

Leave a Comment

Your email address will not be published.