$0: Get the filename of the currently executing script, including the path.
[root@test script]# cat 0.sh
#!/bin/bash
echo $0
[root@test script]# sh 0.sh
0.sh
[root@test script]# cat 0.sh
#!/bin/bash
dirname "$0"
basename "$0"
[root@test script]# sh /byrd/script/0.sh
/byrd/script
0.sh
$n: Get the Nth parameter of the currently executed shell script, n=1..9. When n is 0, it means the file name of the script. If n is greater than 9, it is enclosed in curly brackets like ${10}.
[root@test script]# cat n.sh
#!/bin/bash
echo $1 $2 ${10}
[root@test script]# sh n.sh a b c d e f g h i j k l m n
a b j
[root@LAMP script]# sh n.sh {a..z}
a b j
[root@test script]# sh n.sh `seq 11`
1 2 10
$*: Get all parameters of the current shell, treat all command line parameters as a single string.
$@: All parameters of this program "$1" "$2" "$3" "...", this is the best way to pass parameters to other programs, so TA will keep all the parameters embedded in each parameter blank.
$#: Get the total number of arguments in the current shell command line.
[root@test script]# cat hashtag.sh
#!/bin/bash
echo "$#"
[root@test script]# sh hashtag.sh
0
[root@test script]# sh hashtag.sh 1 2 3
3
[root@test script]# sh hashtag.sh `seq 300`
300
[root@test script]# cat example.sh
#!/bin/bash
#Example
if [ $# -ne 2 ];then
echo "Error, please enter two parameters."
exit 1
else
echo "You did a good job."
fi
[root@test script]# sh example.sh a
Error, please enter two parameters.
[root@test script]# sh example.sh a b
You did a good job.
[root@test script]# sh example.sh a b c
Error, please enter two parameters.
$_: represents the last parameter of the previous command
$$: represents the PID of the current command
[root@LAMP script]# cat dollar.sh
#!/bin/bash
echo "$$" >/tmp/dollar.pid
while true
do
sleep 1
done
[root@LAMP script]# sh dollar.sh
################################################
[root@LAMP ~]# cat /tmp/dollar.pid
1483
[root@LAMP ~]# ps -ef |grep 1483
root 1483 1453 0 14:58 pts/1 00:00:00 sh dollar.sh
root 1532 1483 0 14:58 pts/1 00:00:00 sleep 1
root 1534 1496 0 14:58 pts/0 00:00:00 grep 1483
[root@LAMP ~]# ps -ef |grep dollar
root 1483 1453 0 14:58 pts/1 00:00:00 sh dollar.sh
root 1555 1496 0 14:58 pts/0 00:00:00 grep dollar
$!: Represents the PID of the last executed background command
$?: Represents whether the previous command was successfully executed or not. If the execution is successful, $? is 0, otherwise it is not 0
[byrd@LAMP script]$ pwd
/byrd/script
[byrd@LAMP script]$ echo $?
0 #运行成功
[byrd@LAMP script]$ ls /root
ls: cannot open directory /root: Permission denied
[byrd@LAMP script]$ echo $?
2 #权限拒绝
[byrd@LAMP script]$ hahaha
-bash: hahaha: command not found
[byrd@LAMP script]$ echo $?
127 #The command was not found
###########################################
[byrd@LAMP ~]$ cat /byrd/script/question_mark.sh
#!/bin/bash
#Example
ls -al /root >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "User is root"
else
echo "The user is not root"
fi
[root@LAMP script]# sh question_mark.sh
User is root
[root@LAMP script]# su - byrd
[byrd@LAMP ~]$ sh /byrd/script/question_mark.sh
The user is not root
未完成,待整理!