shell 杂烩

xiaoxiao2025-09-15  23

shell abc guide

开头写入解释器#!/bin/bash。以.sh结尾开头加入个人信息、版本信息 ~/.vimrc注释少用中文中括号两端要带 空格 成对内容一次写出来( ) {} 【】 “” 流程语句一次写完; if 条件 then 内容 fi for do 内容 done 代码缩进,提高易读性 echo $SHELL /bin/bash **特殊变量** $0 #Shell本身的文件名,包括路径 $1~$n 添加到Shell的各参数值。$1是第1参数、$2是第2参数…。 $* 所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 (用的不多) $@ 所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 $# 添加到Shell的参数个数(第0个不算) #**进程状态变量** $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) 0-成功,2-权限被拒绝,127-命令未找到,0-126 失败 $- 使用Set命令设定的Flag一览 3种清空方法 cat /dev/null > your_file.xxx # 清空(保留文件,清空内容) > your_file.xxx echo >your_file.xxx

shell 中报错,0: command not found ,if [ -------------- ] 判断条件前后加空格; [] 中括号两端留空格

1>&2和2>&1 代表什么?

在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是:

0: Standard Input (STDIN)

1: Standard Output (STDOUT)

2: Standard Error Output (STDERR)

在标准情况下, 这些FD分别跟如下设备关联:

stdin(0): keyboard 键盘输入,并返回在前端

stdout(1): monitor 正确返回值 输出到前端

stderr(2): monitor 错误返回值 输出到前端

举例说明吧:

当前目录只有一个文件 a.txt.

[root@redhat box]# ls

a.txt

[root@redhat box]# ls a.txt b.txt

ls: b.txt: No such file or directory 由于没有b.txt这个文件, 于是返回错误值, 这就是所谓的2输出

a.txt 而这个就是所谓的1输出

再接着看:

[root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err

执行后,没有任何返回值. 原因是, 返回值都重定向到相应的文件中了,而不再前端显示

[root@redhat box]# cat file.out

a.txt

[root@redhat box]# cat file.err

ls: b.txt: No such file or directory

一般来说, “1>” 通常可以省略成 “>”.

即可以把如上命令写成: ls a.txt b.txt >file.out 2>file.err

有了这些认识才能理解 “1>&2” 和 “2>&1”.

1>&2 正确返回值传递给2输出通道 &2表示2输出通道

如果此处错写成 1>2, 就表示把1输出重定向到文件2中.

2>&1 错误返回值传递给1输出通道, 同样&1表示1输出通道.

举个例子.

[root@redhat box]# ls a.txt b.txt 1>file.out 2>&1

[root@redhat box]# cat file.out

ls: b.txt: No such file or directory

a.txt

现在, 正确的输出和错误的输出都定向到了file.out这个文件中, 而不显示在前端.

补充下, 输出不只1和2, 还有其他的类型, 这两种只是最常用和最基本的.

cd /var/your_dir || { echo " Cannot chage to your_dir!" >&2 exit 1 }

变量 环境变量 ______ 默认环境变量 大写; export 导出的变量;env/printenv .bashrc .bash_profile /etc/bashrc /etc/bash_profile /etc/profile.d/

本地变量 name=2018 # =前后不可以有空格,否则变量定义不成功;内容一般为简单连续的数字或者字符串,路径名等,不带空格等 name1=‘2018’ #单引号中看到什么就是什么,不会解析,双引号会,适用与纯字符串,可含有空格 name2=“2018” # 双引号中的变量会被解析,适合于字符串中带有变量的内容的定义,可有空格等 echo $name echo $name1 echo $name2 # 输出相同

cat local_var.sh #!/bin/bash name=2018 name1='2018-$name' name2="2018-$name" echo $name echo "b = $name1" echo "c = $name2" robot@ubuntu:~/shell_prac$ ./local_var.sh 2018 b = 2018-$name c = 2018-2018

使用变量时,使用${name} , 大括号中不可有空格,!!!是大括号不是圆括号

将命令定义为变量——反引号

cmd = `ll` echo $cmd charr=`seq -s " " 10` echo $charr 1 2 3 4 5 6 7 8 9 10 echo ${#charr} #取变量长度 20 echo $charr|wc -m 21

将命令定义为变量——$(命令)

cmd=$(PWD) echo $cmd

变量的数值计算 $(( )) 最常用

参数类型必须是整数,不能是小数或者字符串bc命令可以进行浮点运算((( )) 里面可以有空格,也可以没有 ((a = 1+2**3 - 4%3)) robot@ubuntu:~/shell_prac$ $a 8: command not found robot@ubuntu:~/shell_prac$ echo $a 8 robot@ubuntu:~/shell_prac$ echo $((a+=2)) 10 echo $((3>9)) 0 ########## #!/bin/bash a=3 b=3 echo "a+b= $(( a + b))" echo "a*b=$((a*b))" echo "a**b=$((a**b))" echo "a%b=$((a%b))" ##括号中引用a,b也没有用$取 #########命令行传参的方式 #!/bin/bash a=$1 b=$2 echo "a+b= $(( a + b))" echo "a*b=$((a*b))" echo "a**b=$((a**b))" echo "a%b=$((a%b))" ####计算器实现加减乘除 echo $(($1$2$3)) ####2 #!/bin/bash #add,subtract,multiply and divide print_usage(){ printf "Please enter an integer \n" } read -p "please input first number:" firstnum read -p "please input the operator:" operators read -p "please input second number:" secondnum echo $(( $firstnum $operators $secondnum ))

方法2:let

robot@ubuntu:~/shell_prac$ i=2 robot@ubuntu:~/shell_prac$ i=i+8 robot@ubuntu:~/shell_prac$ echo $i i+8 robot@ubuntu:~/shell_prac$ i=2 robot@ubuntu:~/shell_prac$ let i=i+8 robot@ubuntu:~/shell_prac$ echo $i 10

方法3:expr

计算符 和数字之间要有空格使用*号,要用\转义 robot@ubuntu:~/shell_prac$ expr 2 + 2 4 robot@ubuntu:~/shell_prac$ expr 2 + 2 4 robot@ubuntu:~/shell_prac$ expr 2 -1 expr: syntax error robot@ubuntu:~/shell_prac$ expr 2+2 2+2 robot@ubuntu:~/shell_prac$ expr 2 * 2 expr: syntax error robot@ubuntu:~/shell_prac$ expr 2 /* 2 expr: syntax error robot@ubuntu:~/shell_prac$ expr 2 / 2 1 robot@ubuntu:~/shell_prac$ expr 2 // 2 expr: syntax error robot@ubuntu:~/shell_prac$ echo "expr length "ubuntu"" expr length ubuntu robot@ubuntu:~/shell_prac$ echo $(expr length "ubuntu") 6 i=`expr $i + 1.2` expr: non-integer argument robot@ubuntu:~/shell_prac$ i=`expr $i + 1` robot@ubuntu:~/shell_prac$ echo i=`expr $i + 1` i=2 expr $[2+4] 6 #上面不用空格

【】 中括号

单中括号[ ]:

字符串比较——==和!=

整数比较——不等于:-gt:大于;-lt :小于;-eq:等于;-ne

数组索引——array[0]

bc计算器 bc unix计算器,支持小数 echo 5.4+4|bc echo 1 + 3.2 | bc seq -s “+” 10 | bc 5050

read 语法格式 read [参数] [变量名] 常用参数:-p -t -n read -t 4 -p “pls input two number in 4 seconds” a1 a2 echo $a1 $a2 read -p 的功能可用echo read 组合替代 echo -n “please input two number” ##-n 不换行输出 read a1 a2

条件测试

test <条件表达式>[<条件表达式>] #常用[[<条件表达式>]]

test

test -f std_err.sh && echo 1 || echo 0 # -f 文件类型 1 test ! -f std_err.sh && echo 1 || echo 0 # ! 取反 0

[<条件表达式>] #中口号首末两端要有空格

[ std_err.sh ] && echo 1 || echo 0 1 [ -f std_err.sh ] && echo 1 || echo 0 1 [ -f file ] && cat file || touch file

[[ ]] #双中括号

robot@ubuntu:~/shell_prac$ [ -f file ] && cat file robot@ubuntu:~/shell_prac$ [[ -f file ]] && cat file robot@ubuntu:~/shell_prac$ [[ ! -f file ]] && cat file cat: file: No such file or directory robot@ubuntu:~/shell_prac$ [[ ! -f file && -d folder ]] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ ! -f file && -d folder ] && echo 1 || echo 0 bash: [: missing `]' 0 robot@ubuntu:~/shell_prac$ [ ! -f file -a -d folder ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ -f file -a -d folder ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ touch file; mkdir folder robot@ubuntu:~/shell_prac$ [ -f file -a -d folder ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [[ -f file && -d folder ]] && echo 1 || echo 0 1

文件条件测试 字符串测试操作符 整数二元比较操作符

robot@ubuntu:~/shell_prac$ [ 2 > 1 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [[ 2 > 1 ]] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ 2 < 1 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ 2 \< 1 ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [[ 2 < 1 ]] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ 2 -gt 1 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ 2 -lt 1 ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ 2 -ne 1 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ 2 = 1 ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ 2 == 1 ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ 2 == 2 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ 2 != 2 ] && echo 1 || echo 0 0 robot@ubuntu:~/shell_prac$ [ "a" \> "bc" ] && echo 1 || echo 0 0

[ ] 变量用于判断条件时,要加上双引号

robot@ubuntu:~/shell_prac$ [ -f $file2 ] && echo 1 || echo 0 1 robot@ubuntu:~/shell_prac$ [ -f "$file2" ] && echo 1 || echo 0 0

单分支条件 if [ 条件 ] then 指令 fi

if [ 条件 ];then 指令 fi 多分支条件 if [ 条件 ] then 指令 elif [ 条件 ] then 指令 else #else 后面咩有then 指令

转载请注明原文地址: https://www.6miu.com/read-5036357.html

最新回复(0)