CP4 Bash Shell Executing Commands

1.Running Any Executable

Problem: You need to run a command on a Linux or Unix system.

Solution: Use bash and type the name of the command at the prompt.

$ someprog

2.Telling If a Command Succeeded or Not

Problem: You need to know whether the command you ran succeeded.

Solution: The shell variable $? will be set with a non-zero value if the command fails---provided that the programmer who wrote that command or shell script followed the established convention

echo $?

3.Running Several Commands in Sequence

Problem: You need to run several commands,but some take a while and you don't want to wait for the last one to finish before issuing the next command.

Solution1: just keep typing.

Solution2: type those commands into  a file and then tell bash to execute the commands in the file.

Solution3: run each command in sequence,if you want to run each program,regardless if the preceding ones fail,separate them with semicolons:

$ long ; medium ; short

if you only want to run the next program if the preceding program worked,and all the programs correctly set exit codes,separate them with double-ampersands:

$ long && medium && short

4.Running Any Executable

Problem: You need to run three commands,but they are independent of each other,and don't need to wait for each other to complete.

Solution: You can run a command in the background by putting an ampersand(&) at the end of the command line.

$ long & medium & short

5.Deciding Whether a Command Succeeds

Problem: You need to run some commands,but you only want to run certain commands if certain other ones succeed.

Solution: Use bash and type the name of the command at the prompt.

 

6.Using Fewer if Statements

Problem: As a conscientious programmer,you took to heart what we described in the previous recipe.You applied the concept to your latest shell script,and now you find that the shell script is unreadable,if with all those if statements checking the return code of every command.Isn't there an alternative?

Solution: Use the double-ampersand operator in bash to provide conditional execution

 

7.Running Long Jobs Unattended

Problem: You run a job in the background,then exited the shell and went for coffee,When you came back to check,the job was no longer running and it hasn't completed.In fact,your job hadn't progressed very far at all.It seems to have quit as soon as you exited the shell.

Solution: If you want to run a job in the background and expect to exit the shell before the job completes,then you need to nohup the job:

8.Displaying Error Messages When Failures Occur 

Problem: You need your shell script to be verbose about failures.You want to see messages when commands don't work,but if statements tend to distract from the visual flow of statements.

Solution: A common idiom among some shell programmers is to use the || with commands to spit out debug or error messages.

9.Running All Scripts in a Directory

Problem: You want to run a series of scripts ,but the list keeps changing;you're always adding new scripts,but you don't want to continuously modify a master list.

Solution: Put the scripts you want to run in a directory,and let bash run everything,that it finds.Instead of keeping a master list,simply look at the contents of that directory.

for SCRIPT in /path/to/scripts/dir/*

do 

      if [  -f  $SCRIPT -a -x $SCRIPT ]

      then

             $SCRIPT

       fi

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值