Shell脚本整理

-eq 等于,如:if [“ a " e q " a" -eq " b” ]
-ne 不等于,如:if [“ a " n e " a" -ne " b” ]
-gt 大于,如:if [“ a " g t " a" -gt " b” ]
-ge 大于等于,如:if [“ a " g e " a" -ge " b” ]
-lt 小于,如:if [“ a " l t " a" -lt " b” ]
-le 小于等于,如:if [“ a " l e " a" -le " b” ]
-----------------------整数比较

-eq 等于,如:if [ “ a " e q " a" -eq " b” ]
-ne 不等于,如:if [ “ a " n e " a" -ne " b” ]
-gt 大于,如:if [ “ a " g t " a" -gt " b” ]
-ge 大于等于,如:if [ “ a " g e " a" -ge " b” ]
-lt 小于,如:if [ “ a " l t " a" -lt " b” ]
-le 小于等于,如:if [ “ a " l e " a" -le " b” ]

if [ $counter -gt 1 ]; then

fi

大于(需要双括号),如:((“ a " > " a" > " b”))
= 大于等于(需要双括号),如:((“ a " > = " a" >= " b”))
< 小于(需要双括号),如:((“ a &quot; &lt; &quot; a&quot; &lt; &quot; b”))
<= 小于等于(需要双括号),如:((“ a &quot; &lt; = &quot; a&quot; &lt;= &quot; b”))

---------------------------字符串比较
= 等于,如:if [ “ a &quot; = &quot; a&quot; = &quot; b” ]
== 等于,如:if [ “ a &quot; = = &quot; a&quot; == &quot; b” ],与=等价
注意:==的功能在[[]]和[]中的行为是不同的,如下:
1 [[ KaTeX parse error: Expected 'EOF', got '#' at position 12: a == z* ]] #̲ 如果a以"z"开头(模式匹配 )那么将为true
2 [ KaTeX parse error: Expected 'EOF', got '#' at position 13: a == "z*" ] #̲ 如果a等于z*(字符匹配 ),那么结果为true
3
4 [ KaTeX parse error: Expected 'EOF', got '#' at position 11: a == z* ] #̲ File globbing …a" == “z*” ] # 如果$a等于z*(字符匹配),那么结果为true

$0: shell或shell脚本的名字
$*:以一对双引号给出参数列表
$@:将各个参数分别加双引号返回
$#:参数的个数
$_:代表上一个命令的最后一个参数
$$:代表所在命令的PID
$!:代表最后执行的后台命令的PID
$?:代表上一个命令执行后的退出状态

grep 同时满足多个关键字和满足任意关键字

① grep -E “word1|word2|word3” file.txt
满足任意条件(word1、word2和word3之一)将匹配。
② grep word1 file.txt | grep word2 |grep word3
必须同时满足三个条件(word1、word2和word3)才匹配。

1、或操作

grep -E ‘123|abc’ filename // 找出文件(filename)中包含123或者包含abc的行
egrep ‘123|abc’ filename // 用egrep同样可以实现
awk ‘/123|abc/’ filename // awk 的实现方式
2、与操作

grep pattern1 files | grep pattern2 //显示既匹配 pattern1 又匹配 pattern2 的行。
3、其他操作

grep -i pattern files //不区分大小写地搜索。默认情况区分大小写,
grep -l pattern files //只列出匹配的文件名,
grep -L pattern files //列出不匹配的文件名,
grep -w pattern files //只匹配整个单词,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’),
grep -C number pattern files //匹配的上下文分别显示[number]行,

单引 双引 反引用[] [[]]

将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n)

out=$(cat text.txt)

输出1 2 3

out="$(cat text.txt)"

输出:

1

2

3

单引 双引 反引用[] [[]]

将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n)

out=$(cat text.txt)

输出1 2 3

out="$(cat text.txt)"

输出:

1

2

3
1、字符串判断

str1 = str2      当两个串有相同内容、长度时为真
  str1 != str2      当串str1和str2不等时为真
  -n str1        当串的长度大于0时为真(串非空) if [[ -n $1 ]]
  -z str1        当串的长度为0时为真(空串)
  str1         当串str1为非空时为真

2、数字的判断

int1 -eq int2    两数相等为真
  int1 -ne int2    两数不等为真
  int1 -gt int2    int1大于int2为真
  int1 -ge int2    int1大于等于int2为真
  int1 -lt int2    int1小于int2为真
  int1 -le int2    int1小于等于int2为真

3 目录文件的判断(if [ ])

-r file     用户可读为真
  -w file     用户可写为真
  -x file     用户可执行为真
  -f file     文件为正规文件为真
  -d file     文件为目录为真
  -c file     文件为字符特殊文件为真
  -b file     文件为块特殊文件为真
  -s file     文件大小非0时为真
  -t file     当文件描述符(默认为1)指定的设备为终端时为真

3、复杂逻辑判断
  -a         与
  -o        或
  !        非
  下面是一些使用实例:

#!/bin/sh
  myPath="/var/log/httpd/"
  myFile="/var /log/httpd/access.log"

#这里的-x 参数判断 m y P a t h     i f [ ! x &quot; myPath是否存在并且是否具有可执行权限   if [ ! -x &quot; myPath"]; then
  mkdir “$myPath”
  fi

#这里的-d 参数判断 m y P a t h     i f [ ! d &quot; myPath是否存在   if [ ! -d &quot; myPath"]; then
  mkdir “$myPath”
  fi

#这里的-f参数判断 m y F i l e     i f [ ! f &quot; myFile是否存在   if [ ! -f &quot; myFile" ]; then
  touch “$myFile”
  fi

#其他参数还有-n,-n是判断一个变量是否是否有值
  if [ ! -n “ m y V a r &quot; ] ; t h e n     e c h o &quot; myVar&quot; ]; then   echo &quot; myVar is empty”
  exit 0
  fi

#两个变量判断是否相等
  if [ “ v a r 1 &quot; = = &quot; var1&quot; == &quot; var2” ]; then
  echo ‘$var1 eq v a r 2     e l s e     e c h o var2&#x27;   else   echo &#x27; var1 not eq $var2’
  fi

----------------- 算数运算 let expr

let 可以直接执行基本的算数操作

no1=4

no2=5

let result=no1+no2

echo $result

let no1++

let no1+=6

操作符[] ,expr 和let命令类似

result=$[ no1 + no2 ]

result=$[ $no1 + $no2 ]

result=expr 3 + 4

result=$(expr $no1 + 5)

================ shell判断文件是否存在

shell判断文件,目录是否存在或者具有权限
  #!/bin/sh

myPath="/var/log/httpd/"
  myFile="/var /log/httpd/access.log"

# 这里的-x 参数判断 m y P a t h     i f [ ! x &quot; myPath是否存在并且是否具有可执行权限   if [ ! -x &quot; myPath"]; then
  mkdir “$myPath”
  fi

# 这里的-d 参数判断 m y P a t h     i f [ ! d &quot; myPath是否存在   if [ ! -d &quot; myPath"]; then
  mkdir “$myPath”
  fi

# 这里的-f参数判断 m y F i l e     i f [ ! f &quot; myFile是否存在   if [ ! -f &quot; myFile" ]; then
  touch “$myFile”
  fi

# 其他参数还有-n,-n是判断一个变量是否是否有值
  if [ ! -n “ m y V a r &quot; ] ; t h e n     e c h o &quot; myVar&quot; ]; then   echo &quot; myVar is empty”
  exit 0
  fi

# 两个变量判断是否相等
  if [ “ v a r 1 &quot; = &quot; var1&quot; = &quot; var2” ]; then
  echo ‘$var1 eq v a r 2     e l s e     e c h o var2&#x27;   else   echo &#x27; var1 not eq $var2’
  fi

猜你喜欢

转载自blog.csdn.net/qq_37489565/article/details/84977264