One of the shell scripts: Introduction to shell scripts

1. Introduction to shell scripts (1) Script cases and introduction: #!/bin/bash LOG_DIR=/var/log ROOT_UID=0 if ["$UID -n...
1. Introduction to shell scripts

1. Introduction to shell scripts

(1) Script cases and introduction:

#!/bin/bash LOG_DIR=/var/log ROOT_UID=0 if ["$UID -ne "$ROOT_UID"] then echo "must be root run this script." exit 1 fi cd $ LOG_DIR || { echo "cannot change to necessary directory" exit 1 } cat /dev/null>message && { echo "logs cleaned up." exit 0 } echo "logs cleaned fail." exit 1

(2) shell script interpreter:

[root@web01 ~]# echo $SHELL /bin/bash [root@web01 ~]# ll /bin/sh lrwxrwxrwx. 1 root root 4 Oct 10 2018 /bin/sh -> bash

The interpreter defaults to bash, which is called by default if the interpreter is not marked in the script.

Batch comment method 1:ctrl+shift+v, cursor down, shitf+a, first line before#, esc.

Batch Comment Method 2: Write the contents of the comment in the following symbols: < < EOF XXX EOF. Colon means do nothing.

Bulk Annotation Method 3:cat >/dev/null< EOF XXX EOF

(3) shell execution methods:

Method 1:bash,sh

Method 2:/path/script-name or. /script-name

Method 3: source script-name or.script-name

Method 4:sh<script-name or cat scripts-name | sh

Example:

[root@web01 scripts01]# bash test.sh i am oldboy teacher. [root@web01 scripts01]# sh test.sh i am oldboy teacher. [root@web01 scripts01]# ./test.sh -bash: ./test.sh: Permission denied [root@web01 scripts01]# /server/scripts01/test.sh -bash: /server/scripts01/test.sh: Permission denied [root@web01 scripts01]# ls -l test.sh -rw-r--r-- 1 root root 127 Jan 18 23:12 test.sh [root@web01 scripts01]# chmod +x test.sh [root@web01 scripts01]# /server/scripts01/test.sh i am oldboy teacher. [root@web01 scripts01]# sh < test.sh i am oldboy teacher. [root@web01 scripts01]# cat test.sh | sh i am oldboy teacher. [root@web01 scripts01]#

Example:

[root@web01 scripts01]# chkconfig --list | grep 3:on | awk '' chkconfig crond off chkconfig network off chkconfig rsyslog off chkconfig sshd off [root@web01 scripts01]# chkconfig --list | grep 3:on | awk '' | bash [root@web01 scripts01]# chkconfig --list | grep 3:on

Method 3: source script-name or.script-name

[root@web01 scripts01]# cat test1.sh user=`whoami` [root@web01 scripts01]# sh test1.sh [root@web01 scripts01]# echo $user [root@web01 scripts01]# # sh is equivalent to opening a new subshell under the current shell, so echo $user executes under the current open shell. [root@web01 scripts01]# source test1.sh [root@web01 scripts01]# echo $user root [root@web01 scripts01]# #source is equivalent to executing under the current shell. Parent shell Subshell Using source and.To execute a script is equivalent to executing a script in a shell and can be called from one another. Use bash or sh to execute a script, open a new shell, or open a child shell. [root@web01 scripts01]# vim 1.sh sh test1.sh echo $user [root@web01 scripts01]# sh 1.sh [root@web01 scripts01]# cat 1.sh sh test1.sh echo $user [root@web01 scripts01]# cat 1.sh . ./test1.sh echo $user [root@web01 scripts01]# sh 1.sh root Parent and child shell s can be called from one another using source and.

(4) shell execution process:

Parent shell script-External command-Subscript-Parent shell

Parent shells and child shells cannot call each other: use source and point. to execute if you want to call each other.

shell scripting specifications and habits:

1 Begin with interpreter: #!/bin/bash

(2) Author and copyright information attached: Oldboy at dddddd

(3) Script extension:.sh

(4) Place scripts in a fixed location: /server/scripts

Chinese is not used in scripts.

Paired symbols are written at once.Brackets, space required on both sides

Loop format input completed at one time.

27 April 2020, 14:02 | Views: 8576

Add new comment

For adding a comment, please log in
or create account

0 comments