当前位置:网站首页>Chapter 4 Executing Commands
Chapter 4 Executing Commands
2022-07-23 04:11:00 【梦想家DBA】
The main purpose of bash (or any shell) is to allow you to interact with the computer's operating system so that you can accomplish whatever you need to do.Usually that involves lauching programs, so the shell takes the commands you type, determines from that input what programs nee to be run, and lauches them for you.
1.1 Running Any Executable
Use bash and type the name of the command at the prompt
$ someprog
[[email protected] test]$ echo $PATH
/home/maxwell/.local/bin:/home/maxwell/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[[email protected] test]$ 1.2 Telling If a Command Succeeded or Not
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.
$ somecommand
it works...
$ echo $?
0
$ badcommand
it fails...
$ echo $?
1
$1.3 Running Serveral Commands in Sequence
$ cat > simple.script
long
medium
short
^D # Ctrl-D, not visible
$ bash ./simple.script
# The third, and arguably best,solution is to 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 && short1.4 Running Serveral Commands All at Once
You can run a command in the background by putting an ampersand (&) at the end of the command line.
[[email protected] test]$ long & medium & short
[1] 181971
[2] 181972
-bash: long: command not found
-bash: short: command not found
-bash: medium: command not found
[1]- Exit 127 long
[2]+ Exit 127 medium
[[email protected] test]$
[[email protected] test]$ 1.5 Deciding Whether a Command Succeeds
We can use the exit status ($?) of the cd command in combination with an if statement to di the rm only if the cd was successful.
[[email protected] test]$ ls -ltr
total 64
-rw-rw-r-- 1 maxwell maxwell 10 Jul 21 15:39 a.out
-rw-rw-r-- 1 maxwell maxwell 19 Jul 21 15:40 cong.txt
-rw-rw-r-- 1 maxwell maxwell 15 Jul 21 15:40 def.conf
-rw-rw-r-- 1 maxwell maxwell 20 Jul 21 15:40 file.txt
-rw-rw-r-- 1 maxwell maxwell 21 Jul 21 15:41 more.txt
-rw-rw-r-- 1 maxwell maxwell 35 Jul 21 15:42 zebra.list
-rw-rw-r-- 1 maxwell maxwell 36 Jul 21 20:03 lines
-rw-rw-r-- 1 maxwell maxwell 49 Jul 21 20:43 asdfwednnrw2rs2esff.txt
-rw-rw-r-- 1 maxwell maxwell 69 Jul 21 21:09 one.file
-rw-rw-r-- 1 maxwell maxwell 65 Jul 21 21:10 another.file
-rwxrwxr-x 1 maxwell maxwell 126 Jul 22 11:51 ext.sh
-rwxrwxr-x 1 maxwell maxwell 123 Jul 22 12:01 donors.sh
-rw-rw-r-- 1 maxwell maxwell 227 Jul 22 13:27 myscript.sh
-rw-rw-r-- 1 maxwell maxwell 838 Jul 22 16:12 func_choose.sh
-rw-rw-r-- 1 maxwell maxwell 576 Jul 22 16:25 func_choice1.sh
-rw-rw-r-- 1 maxwell maxwell 765 Jul 22 16:36 select_dir.sh
drwxrwxr-x 2 maxwell maxwell 58 Jul 22 17:42 sample
[[email protected] test]$ if cd sample; then rm * ; fi
[[email protected] sample]$ ls -ltr
total 0
[[email protected] sample]$ touch a.txt b.txt c.txt
[[email protected] sample]$ ls -ltr
total 0
-rw-rw-r-- 1 maxwell maxwell 0 Jul 22 18:46 c.txt
-rw-rw-r-- 1 maxwell maxwell 0 Jul 22 18:46 b.txt
-rw-rw-r-- 1 maxwell maxwell 0 Jul 22 18:46 a.txt
[[email protected] sample]$ cd ..
[[email protected] test]$ cd sample; if (( $? == 0 )); then rm * ; fi
[[email protected] sample]$ ls -ltr
total 0
[[email protected] sample]$ 4.6 Using Fewer if Statements
[[email protected] test]$ pwd
/home/maxwell/shelllearning/test
[[email protected] test]$ ls /home/maxwell/shelllearning/test/sample
a.txt b.txt c.txt d.txt
[[email protected] test]$ cd sample && rm *; ls -ltr
total 0
[[email protected] sample]$ ls -ltr
total 0
[[email protected] sample]$ 1.7 Running Long Jobs Unattended
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:
[[email protected] test]$ nohup long &
[1] 182259
[[email protected] test]$ nohup: ignoring input and appending output to 'nohup.out'
nohup: failed to run command 'long': No such file or directory
[1]+ Exit 127 nohup long
[[email protected] test]$ The nohup command simply sets up the child process to ignore hang-up signals. You can still kill a job with the kill command, because kill sends a SIGTERM signal not a SIGHUP signal. But with nohup, bash won’t inadvertently kill your job when you exit. The message that nohup gives about appending your output is just nohup trying to be helpful.
1.8 Displaying Error Messages When Failures Occur
A common idiom among some shell programmers is to use the || with commands to spit out debug or error messages.
[[email protected] test]$ cmd || printf "%b" "cmd failed. You're on your own\n"
-bash: cmd: command not found
cmd failed. You're on your own
[[email protected] test]$ cmd || { printf "%b" "FAILED.\n" ; exit 1 ; }
-bash: cmd: command not found
FAILED.
logout
[[email protected] ~]#
Due to an oddity of bash syntax, the semicolon after the last command and just before the } is required, and that closing brace must be separated by whitespace from the surrounding text.
1.9 Running Commands from a Variable
[[email protected] ~]# ls -ltr /tmp/
total 28
drwxr-xr-x 2 root root 6 Jul 18 21:44 dir1
drwxr-xr-x 2 root root 6 Jul 18 21:44 dir2
-rw-r--r-- 1 root root 14 Jul 21 13:32 sample_file
-rw-rw-r-- 1 maxwell maxwell 15 Jul 21 15:35 echo.out
-rw-rw-r-- 1 maxwell maxwell 58 Jul 21 15:44 save.out
-rw-rw-r-- 1 maxwell maxwell 142 Jul 21 19:57 ls.out
-rw-rw-r-- 1 maxwell maxwell 213 Jul 21 20:52 all.out
-rw-rw-r-- 1 maxwell maxwell 194 Jul 21 20:57 alla.out
-rw-rw-r-- 1 maxwell maxwell 134 Jul 21 21:10 cat.out
[[email protected] ~]# FN=/tmp/cat.out
[[email protected] ~]# PROG=echo
[[email protected] ~]# $PROG $FN
/tmp/cat.out
[[email protected] ~]# PROG=cat
[[email protected] ~]# $PROG $FN
1
23
345
4567
56789
678910
sdjdasfa
sadjajdfc
dfjajdjado
adjajnsojqo
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
[[email protected] ~]#1.10 Running All Scripts in a Directory
Put the scripts you want to run in a directory, and let bash run everything that if finds.
for SCRIPT in /path/to/scripts/dir/*
do
if [ -f $SCRIPT -a -x $SCRIPT ]
then
$SCRIPT
fi
done边栏推荐
- Richview textbox items textbox
- 32.< tag-数组和位运算>补充: lt.剑指 Offer 56 - I. 数组中数字出现的次数
- Chrome selenium uses the default profile without emptying it every time
- 信息安全危机四伏,企业数据资产泄露管控刻不容缓
- MySQL基础篇(运算符、排序分页、多表查询、函数)
- Redis transaction, lock mechanism, seckill
- 赛尔运维:高校IT运维服务新样本
- Flask学习笔记
- Redis installation
- How switch statements work
猜你喜欢

mysql通过sql查询数据库所有表名称及列信息

第四篇章:运行时数据区——共享空间

chrome selenium 用默认profile 不必每次清空

SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?

Performance introduction

Sonar中如何删除一个项目

客户至上 | 国产BI领跑者,思迈特软件完成C轮融资

performance介绍

Chrome selenium uses the default profile without emptying it every time

Use and implementation of enumeration classes
随机推荐
22、wpf之Combobox使用小记
chrome selenium 用默认profile 不必每次清空
performance介绍
Flask learning notes
Redis transaction - detailed implementation process of seckill case simulation
Kingbasees SQL language reference manual of Jincang database (8. Function (7))
什么是即时通讯?即时通讯的发展
31-spark的算子使用及调度流程
IDEA 集成 Sonar 完整流程
Online English learning system based on s2sh+mysql
Advantages and disadvantages of RDB and AOF
c# 字节数组和类相互转换
MySQL主从复制
UE5 官方案例Lyra全特性详解 6.生成防御塔
2. Judgment statement
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
Redis transaction, lock mechanism, seckill
VirtualBox如何设置端口转发?
腾讯云客户端命令行工具tccli主流程解析
redis的事务、锁机制、秒杀