shell 高级脚本命令

  1. /dev/null - Use to send unwanted output of program

    This is special Linux file which is used to send any unwanted output from program/command.
    Syntax:
    command > /dev/nullExample:
    $ ls > /dev/null

netxu26:~ # ls >/dev/null

 

2. 全局和局部shell变量:

 

Normally all our variables are local. Local variable can be used in same shell, if you load another copy of shell (by typing the /bin/bash at the $ prompt) then new shell ignored all old shell's variable. For e.g. Consider following example
$ vech=Bus
$ echo $vech
Bus
$ /bin/bash
$ echo $vech

NOTE :-Empty line printed
$ vech=Car
$ echo $vech

Car
$ exit # Exit from second shell return to first shell
$ echo $vech
Bus

 

shell中定义全局变量:

 

To set global varible you have to use export command.
Syntax:
export variable1, variable2,.....variableN

 

2 shell中的条件:

 

The control operators are && (read as AND) and || (read as OR). The syntax for AND list is as follows
Syntax:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.
The syntax for OR list as follows
Syntax:
command1 || command2
command2 is executed if and only if command1 returns a non-zero exit status.

 

Syntax:
command1 && comamnd2 if exist status is zero || command3 if exit status is non-zero
if command1 is executed successfully then shell will run command2 and if command1 is not successful then command3 is executed.

 

3 输入输出重定向:

n Linux (And in C programming Language) your keyboard, screen etc are all treated as files. Following are name of such files

Standard File File Descriptors number Use Example
stdin0as Standard input Keyboard
stdout1as Standard output Screen
stderr2as Standard error Screen

By default in Linux every program has three files associated with it, (when we start our program these three files are automatically opened by your shell). The use of first two files (i.e. stdin and stdout) , are already seen by us. The last file stderr (numbered as 2) is used by our program to print error on screen. You can redirect the output from a file descriptor directly to file with following syntax
Syntax:
file-descriptor-number>filename

 

netxu26:~ # rm test
rm: cannot remove `test': No such file or directory
netxu26:~ # rm test>er
rm: cannot remove `test': No such file or directory
netxu26:~ # cat er
netxu26:~ # rm test 2>er
netxu26:~ # cat er
rm: cannot remove `test': No such file or directory

 

The 1>&2 directs the standard output (stdout) to standard error (stderr) device.

 

4 shell中的函数:

To define function use following syntax:
Syntax:

           function-name ( )

{

command1

command2

.....

...

commandN

return



将function写入 /etc/bashrc可以永久保存。


}

Where function-name is name of you function, that executes series of commands. A return statement will terminate the function

 

5 trap command:

以下这一部分为转载:

trap命令用于指定在接收到信号后将要采取的行动。
trap命令的一种常见用途是在脚本程序被中断时完成清理工作。历史上,shell总是用数字来代表信号,而新的脚本程序应该使用信号的名字。你可以在命令提示符下输入命令trap -l来查看信号编号及其关联的名称,在使用信号名时需要省略SIG前缀。
trap命令的参数分为两部分,前一部分是接收到指定信号时将要采取的行动,后一部分是要处理的信号名
trap command signal
如果要重置某个信号的处理条件到其默认值,只需简单的将command设置为-。如果要忽略某个信号,就把command设置为空字符串‘’。一个不带参数的trap命令将列出当前设置的信号及其行动的清单。

注意:对于类似INT的信号,如果捕获后,在trap设置的命令中不提供退出机制,程序将无法通过 Ctl+C 停止。

X/Open规范里面规定的能够被捕获的比较重要的一些信号(括号里面的数字是传统的信号编号):

信     号

说     明

HUP(1)

挂起,通常因终端掉线或用户退出而引发

INT(2)

中断,通常因按下Ctrl+C组合键而引发

QUIT(3)

退出,通常因按下Ctrl+/组合键而引发

ABRT(6)

中止,通常因某些严重的执行错误而引发

ALRM(14)

报警,通常用来处理超时

TERM(15)

终止,通常在系统关机时发送

 

netxu26:/etc # trap -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

 

 

6 getopt command:

This command is used to check valid command line argument are passed to script . Usually used in while loop.
Syntax:
getopts {optsring} {variable1}

getopts is used by shell to parse command line argument.
As defined in man pages:
"optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable variable1, When an option requires an argument, getopts places that argument into the variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen, getopts places ? into variable1."

 

贴个例子吧,这种东西用起来就会了,python,perl中都有类似的东西:

vi ani
#
# Usage: ani -n -a -s -w -d
#
#
# help_ani() To print help
#
help_ani()
{
  echo "Usage: $0 -n -a -s -w -d"
  echo "Options: These are optional argument"
  echo " -n name of animal"
  echo " -a age of animal"
  echo " -s sex of animal "
  echo " -w weight of animal"
  echo " -d demo values (if any of the above options are used "
  echo " their values are not taken)"
  exit 1
}
#
#Start main procedure
#
#
#Set default value for variable
#
isdef=0
na=Moti
age="2 Months" # may be 60 days, as U like it!
sex=Male
weight=3Kg
#
#if no argument
#
if [ $# -lt 1 ]; then
  help_ani
fi
while getopts n:a:s:w:d opt
do
  case "$opt" in
    n) na="$OPTARG";;
    a) age="$OPTARG";;
    s) sex="$OPTARG";;
    w) weight="$OPTARG";;
    d) isdef=1;;
    /?) help_ani;;
  esac
done
if [ $isdef -eq 0 ]
then
  echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"
else
  na="Pluto Dog"
  age=3
  sex=Male
  weight=20kg
  echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
fi

 

6 shift 命令:

The shift command moves the current values stored in the positional parameters (command line args) to the left one position. For example, if the values of the current positional parameters are:

$1 = -f $2 = foo $3 = bar
and you executed the shift command the resulting positional parameters would be as follows:

$1 = foo $2 = bar

For e.g. Write the following shell script to clear you idea:

$ vi shiftdemo.sh
echo "Current command line args are: /$1=$1, /$2=$2, /$3=$3"
shift
echo "After shift command the args are: /$1=$1, /$2=$2, /$3=$3"

Excute above script as follows:
$ chmod +x shiftdemo.sh
$ ./shiftdemo -f foo bar
Current command line args are: $1=-f, $2=foo, $3=bar
After shift command the args are: $1=foo, $2=bar, $3=

You can also move the positional parameters over more than one place by specifying a number with the shift command. The following command would shift the positional parameters two places:

shift 2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值