echo是用于终端打印的最基本命令。
[root@localhost]$ echo "Hello shell world" Hello shell worldecho可选项
echo [option]
-n 默认情况下, echo会在输出文本的尾部追加一个换行符。可以使用选项-n来禁止这种行为。
例如:
[root@localhost] echo -n "Hello shell world" Hello shell world[root@localhost]$
-e 选项可以打印包含转义序列的字符串。
例如:
$ echo -e "1t2t3t4" 1 2 3 4打印彩色输出
- 脚本可以使用转义序列在终端中生成彩色文本。
- 文本颜色是由对应的色彩码来描述的。其中包括:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,洋红=35,青色=36,白色=37。
[root@localhost shell_learning]$ echo -e "e[1;31m This is red text e[0m" This is red text #终端里是红色的
- 上述脚本其中 e[1;31m 是一个转义字符串,可以将颜色设为红色, e[0m 将颜色重新置回。只需要将31替换成想要的色彩码就可以了。
- 对于彩色背景,经常使用的颜色码是:重置=0,黑色=40,红色=41,绿色=42,黄色=43, 蓝色=44,洋红=45,青色=46,白色=47。
env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36: SSH_CONNECTION=192.1.x.xxx 52994 192.1.2.59 22 LANG=en_US.UTF-8 HISTCONTROL=ignoredups DISPLAY=localhost:10.0 HOSTNAME= XDG_SESSION_ID=1 USER=root PWD=/xxx/nbc/NBC/shell_learning HOME=/root SSH_CLIENT=192.1.3.212 52992 22 SSH_TTY=/dev/pts/1 MAIL=/var/spool/mail/root TERM=xterm SHELL=/bin/bash SHLVL=1 LOGNAME=root DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus XDG_RUNTIME_DIR=/run/user/0 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin HISTSIZE=1000 _=/usr/bin/env OLDPWD=/xxx/nbc/NBC补充内容 获得字符串的长度
length=${#var}
[root@localhost shell_learning]$ val=1234567890
[root@localhost shell_learning]$ echo ${#val}
10
识别当前所使用的shell
[root@localhost shell_learning]$ echo $SHELL /bin/bash
或者
[root@localhost shell_learning]$ echo $0 -bashlet、[]、(( ))数值运算和工具expr和bc执行高级操作 let命令 let可以直接执行数值运算
[root@localhost shell_learning]$ no1=10 [root@localhost shell_learning]$ no2=20 [root@localhost shell_learning]$ let result=no1+no2 [root@localhost shell_learning]$ echo $result 30let自加操作
[root@localhost shell_learning]$ let no1++let自减操作
[root@localhost shell_learning]$ let no1--简写形式
let no1+=6 let no2-=6其他方式 操作符[ ]
[root@localhost shell_learning]$ no1=10 [root@localhost shell_learning]$ no2=20 [root@localhost shell_learning]$ result=$[ no1 + no2 ] [root@localhost shell_learning]$ echo $result 30
或者
[root@localhost shell_learning]$ no1=10 [root@localhost shell_learning]$ result=$[ $no1 + 20 ] [root@localhost shell_learning]$ echo $result 30操作符(( ))
[root@localhost shell_learning]$ no1=10 [root@localhost shell_learning]$ result=$(( no1 + 20 )) #"(())"前必须加"$" [root@localhost shell_learning]$ echo $result 30expr数值操作
[root@localhost shell_learning]$ result=`expr 3 + 4 ` [root@localhost shell_learning]$ echo $result 7
或者
[root@localhost shell_learning]$ result=$(expr $no1 + 5) [root@localhost shell_learning]$ echo $result 15
以上这些方法不支持浮点数,只能用于整数运算。
bc可执行浮点数运算[root@localhost shell_learning]$ echo "4 * 0.56" | bc 2.24 [root@localhost shell_learning]$ no=54 [root@localhost shell_learning]$ result=`echo "$no * 1.5" | bc ` [root@localhost shell_learning]$ echo $result 81.0设定小数精度
[root@localhost shell_learning]$ echo "scale=3;22/7" | bc 3.142进制转换
例:十进制转二进制。
[root@localhost shell_learning]$ no=100 [root@localhost shell_learning]$ echo "obase=2;$no" | bc 1100100计算平方以及平方根
[root@localhost shell_learning]$ echo "sqrt(100)" | bc #开平方 10 [root@localhost shell_learning]$ echo "10^10" | bc #平方 10000000000文件描述符
文件描述符是与某个打开的文件或数据流相关联的整数。 文件描述符0、 1以及2是系统预留的。
- 0 —— stdin (标准输入)。
- 1 —— stdout(标准输出)。
- 2 —— stderr(标准错误)。
如:
[root@localhost shell_learning]$ echo "This is a sample text 1" > temp.txt2、使用双大于号(>>)将文本追加到文件中
如:
[root@localhost shell_learning]$ echo “This is a sample text 2” >> temp.txt
如:
[root@localhost shell_learning]$ cat temp.txt This is a sample text 1 This is a sample text 2$? 打印命令退出状态
- 当一个命令发生错误并退回时,它会返回一个非0的退出状态;而当命令成
功完成后,它会返回为0的退出状态。退出状态可以从特殊变量$?中获得(在命
令结束之后立刻运行echo $?,就可以打印出退出状态)。
在下面的命令中,我们使用2>(数字2以及大于号)将stderr重定向到out.txt
[root@localhost shell_learning]$ ls + 2> out.txt [root@localhost shell_learning]$ cat out.txt ls: cannot access '+': No such file or directorycat显示文本内容
cat [option]
-n选项可以从stdin中接收到的每一行数据前加上行号并将其写入stdout。
例如:
[root@localhost shell_learning]$ cat -n out.txt
1 A1
2 A2
3 A3
4 arg1
5 arg2
6 arg3
-s,终端显示时,删除这些额外的、多余的空白行。
例如:
[root@localhost shell_learning]$ cat -n multi_blanks.txt
1 line1
2
3 line2
4
5
6 line3
7
8
9
10
11
12 line4
[root@localhost shell_learning]$ cat -n -s multi_blanks.txt
1 line1
2
3 line2
4
5 line3
6
7 line4
-T选项能够将制表符标记成^I
例如:
[root@localhost shell_learning]$ cat -n multi_blanks.txt
1 line1
2
3 line2
4
5
6 line3
7
8
9
10
11
12 line4
[root@localhost shell_learning]$ cat -nTs multi_blanks.txt
1 line1
2
3 ^Iline2
4
5 ^I^Iline3
6
7 ^I^I^Iline4
数组和关联数组
数组定义
1、可以在单行中使用数值列表来定义一个数组
[root@localhost shell_learning]$ array_var=(test1 test2 test3 test4)2、另外,还可以将数组定义成一组“索引-值”
[root@localhost shell_learning]$ array_var[0]="test1" [root@localhost shell_learning]$ array_var[1]="test2" [root@localhost shell_learning]$ array_var[2]="test3" [root@localhost shell_learning]$ array_var[3]="test4" [root@localhost shell_learning]$ array_var[4]="test5" [root@localhost shell_learning]$ array_var[5]="test6"打印出数组元素的内容
例如:
1、打印出特定索引的数组元素内容[root@localhost shell_learning]$ echo ${array_var[2]}
test3
2、以列表形式打印出数组中的所有值
[root@localhost shell_learning]$ echo ${array_var[*]}
test1 test2 test3 test4
或
[root@localhost shell_learning]$ echo ${array_var[@]}
test1 test2 test3 test4
3、打印数组长度(即数组中元素的个数)
[root@localhost shell_learning]$ echo ${#array_var[*]}
4
关联数组定义
关联数组声明
[root@localhost shell_learning]$ declare -A ass_array使用行内“索引-值”列表定义
例如:
[root@localhost shell_learning]$ fruits_value=([apple]='100 dollars' [orange]='150 dollars')
[root@localhost shell_learning]$ echo "Apple costs ${fruits_value[apple]}"
Apple costs 100 dollars
打印关联数组索引
例:
[root@localhost shell_learning]$ echo ${!fruits_value[*]}
apple orange
或
[root@localhost shell_learning]$ echo ${!fruits_value[@]}
apple orange
alias为命令创建别名
形如:
alias new_command='command sequence'
例如:
使用alias命令用install 替换掉(一般为简化) yum install 命令。
[root@localhost shell_learning]$ alias install='yum install' [root@localhost shell_learning]$ install tree Last metadata expiration check: 1:10:41 ago on Tue 07 Jun 2022 12:41:11 PM CST. Dependencies resolved. ===================================================================================================== Package Architecture Version Repository Size ===================================================================================================== Installing: tree x86_64 1.7.0-15.el8 BaseOS 59 k Transaction Summary ===================================================================================================== Install 1 Package Total download size: 59 k Installed size: 109 k Is this ok [y/N]: y Downloading Packages: tree-1.7.0-15.el8.x86_64.rpm 36 MB/s | 59 kB 00:00 ----------------------------------------------------------------------------------------------------- Total 5.8 MB/s | 59 kB 00:00 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : tree-1.7.0-15.el8.x86_64 1/1 Running scriptlet: tree-1.7.0-15.el8.x86_64 1/1 Verifying : tree-1.7.0-15.el8.x86_64 1/1 Installed: tree-1.7.0-15.el8.x86_64 Complete!



