[root@desktop27 mnt]# vim test.sh
[root@desktop27 mnt]# cat test.sh #!/bin/bashecho \$0 is $0echo \$1 is $1echo \$2 is $2echo \$3 is $3echo \$# is $#echo \$* is $*
echo \$@ is $@
[root@desktop27 mnt]# sh test.sh $0 is test.sh
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is
[root@desktop27 mnt]# sh test.sh westos$0 is test.sh
$1 is westos
$2 is
$3 is
$# is 1
$* is westos
$@ is westos
[root@desktop27 mnt]# sh test.sh westos linux$0 is test.sh
$1 is westos
$2 is linux
$3 is
$# is 2
$* is westos linux
$@ is westos linux
[root@desktop27 mnt]# sh test.sh westos linux redhat$0 is test.sh
$1 is westos
$2 is linux
$3 is redhat
$# is 3
$* is westos linux redhat
$@ is westos linux redhat
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh #!/bin/bashfor name in"$*"doecho$namedone
[root@desktop27 mnt]# sh -x for.sh westos linux redhat
+ for name in'"$*"'
+ echo westos linux redhat
westos linux redhat
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh #!/bin/bashfor name in"$@"doecho$namedone
[root@desktop27 mnt]# sh -x for.sh westos linux redhat
+ for name in'"$@"'
+ echo westos
westos
+ for name in'"$@"'
+ echo linux
linux
+ for name in'"$@"'
+ echo redhat
redhat
[root@desktop27 mnt]#
[root@desktop27 mnt]# vim test.sh
[root@desktop27 mnt]# cat test.sh #!/bin/bashread -p "Please inout a number: " IP
ping -c1 -w1 $IP &> /dev/null && echo$IP is up || echo$IP is down
[root@desktop27 mnt]# sh test.sh
Please inout a number: 172.25.254.50172.25.254.50 is up
[root@desktop27 mnt]# sh test.sh
Please inout a number: 172.25.254.227172.25.254.227 is down
[root@desktop27 mnt]#
linux系統中命令別名的設定——alias
臨時設定
[root@desktop27 ~]# alias xie='vim' ##臨時設定,重新開啟shell后,就會失效
[root@desktop27 ~]# xie ##可以進入vim編輯模式
[root@desktop27 ~]# aliasalias cp='cp -i'
alias egrep='egrep--color=auto'alias fgrep='fgrep--color=auto'alias grep='grep--color=auto'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls--color=auto'alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'alias xie='vim'
[root@desktop27 ~]# logout ##退出當前shell
Connection to172.25.254.127 closed.
[kiosk@desktop50 Desktop]$ ssh root@172.25.254.127 ##重新開啟新shell
root@172.25.254.127's password:
Last login: Sun Jun 1721:25:002018 from foundation35.ilt.example.com
[root@desktop27 ~]# alias ##vim 的別名 xie 已經不存在
alias cp='cp -i'
alias egrep='egrep--color=auto'alias fgrep='fgrep--color=auto'alias grep='grep--color=auto'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls--color=auto'alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@desktop27 ~]# xie
bash: xie: command not found...
[root@desktop27 ~]#
用途說明 exit命令用于退出當前shell,在shell腳本中可以終止當前腳本執行。 常用參數 格式:exit n 退出。設置退出碼為n。(Cause the shell to exit with a status of n.) 格式:exit 退出。退出碼不變,即為最后一個命令的退出碼。(If n is omitted, the exit status is that > of the last command executed. ) 格式:$? 上一個命令的退出碼。 格式:trap “commands” EXIT 退出時執行commands指定的命令。( A trap on EXIT is executed before the shell terminates.) 退出碼(exit status,或exit code)的約定: 0表示成功(Zero - Success) 非0表示失敗(Non-Zero - Failure) 2表示用法不當(Incorrect Usage) 127表示命令沒有找到(Command Not Found) 126表示不是可執行的(Not an executable) 128<= 信號產生
[root@localhost mnt]# vim ip_check.sh
[root@localhost mnt]# cat ip_check.sh #!/bin/bashread -p "Please input IP:" IP
ping -c1 -w1 $IP &> /dev/null
[ "$?"-eq"0" ]&&{ echo-e"\033[34m$IP is up\033[0m"
}||{ echo-e"\033[31m$IP is down\033[0m"
}
[root@localhost mnt]# sh ip_check.sh
Please input IP:172.25.254.127172.25.254.127 is up
[root@localhost mnt]# sh ip_check.sh
Please input IP:172.25.20.229172.25.20.229 is down
[root@localhost mnt]#