微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

常用shell语句

1,# 注释字符(跟python注释字符#一样,可放开头,可放结尾) echo "The # here does not begin a comment." echo 'The # here does not begin a comment.' echo The \# here does not begin a comment. 前三个输出的都一样 echo The # 这里开始一个注释 The echo $(( 2#101011 )) 这个是进制转换 2, ; 分号 使用分号(;)可以在同一行上写两个或两个以上的命令 #!/bin/bash echo hello; echo there filename=ttt.sh if [ -e "$filename" ]; then # 注意: "if"和"then"需要分隔,-e用于判断文件是否存在 echo "File $filename exists."; cp $filename $filename.bak else echo "File $filename not found."; touch $filename fi; echo "File test complete."

0

3,.号 等价于 source 命令 bash 中的 source 命令用于在当前 bash 环境下读取并执行 FileName.sh 中的命令。

0

4,' 和 " 两者待定   5,斜线 1.斜线(/) 文件名路径分隔符。分隔文件名不同的部分(如/home/bozo/projects/Makefile)。也可以用来作为除法算术操作符。注意在linux中表示路径的时候,许多个/跟一个/是一样的。/home/shiyanlou等同于////home///shiyanlou 2.反斜线(\) 一种对单字符的引用机制。\X 将会“转义”字符X。这等价于"X",也等价于'X'。\ 通常用来转义双引号(")和单引号('),这样双引号和单引号就不会被解释成特殊含义了。
  • 符号 说明
  • \n 表示新的一行
  • \r 表示回车
  • \t 表示水平制表符
  • \v 表示垂直制表符
  • \b 表示后退符
  • \a 表示"alert"(蜂鸣或者闪烁)
  • \0xx 转换为八进制的ASCII码, 等价于0xx
  • " 表示引号字面的意思
六、反引号(`) 反引号中的命令会优先执行,如: cp `mkdir back` test.sh back 先创建了 back 目录,然后复制 test.sh 到 back 目录   七、冒号(:) 等价于“nop”(no op,一个什么也不干的命令)。也可以被认为与shell的内建命令true作用相同。“:”命令是一个bash的内建命令,它的退出码(exit status)是(0)。 #!/bin/bash while : do echo "endless loop" done 等价于 #!/bin/bash while true do echo "endless loop" done 可以在 if/then 中作占位符: #!/bin/bash condition=5 if [ $condition -gt 0 ] #gt表示greater than,也就是大于,同样有-lt(小于),-eq(等于) then : # 什么都不做,退出分支 else echo "$condition" fi   八、问号(?) 测试操作符 在一个双括号结构中,? 就是C语言的三元操作符,如: $ vim test.sh 输入如下代码,并保存: #!/bin/bash a=10 (( t=a<50?8:9 )) echo $t 运行测试 $ bash test.sh 8   九、美元符号($) 变量替换 前面已经用到了 $ vim test.sh #!/bin/bash var1=5 var2=23skidoo echo $var1 # 5 echo $var2 # 23skidoo 运行测试 $ bash test.sh 5 23skidoo   一、小括号( ) 1)作为局部变量作用符号 #!/bin/bash a=123 ( a=321; ) echo "$a" #a的值为123而不是321,因为括号将判断为局部变量 2)初始化数组 #!/bin/bash arr=(1 4 5 7 9 21) echo ${arr[3]} # get a value of arr 7   二、大括号 { } 1).文件名扩展 复制 t.txt 的内容到 t.back 中 $ vim test22.sh 输入代码: #!/bin/bash if [ ! -w 't.txt' ]; then touch t.txt fi echo 'test text' >> t.txt cp t.{txt,back} 注意: 在大括号中,不允许有空白,除非这个空白被引用或转义。 2).代码代码块,又被称为内部组,这个结构事实上创建了一个匿名函数一个没有名字的函数)。然而,与“标准”函数不同的是,在其中声明的变量,对于脚本其他部分的代码来说还是可见的。 $ vim test23.sh 输入代码: #!/bin/bash a=123 { a=321; } echo "a = $a" 运行代码: $ bash test23.sh a = 321 变量 a 的值被更改了。   三、中括号[ ] 1.条件测试 条件测试表达式放在[ ]中。下列练习中的-lt (less than)表示小于号。 $ vim test24.sh 输入代码: #!/bin/bash a=5 if [ $a -lt 10 ] then echo "a: $a" else echo 'a>10' fi 运行代码: $ bash test24.sh a: 5 2.数组元素 在一个array结构的上下文中,中括号用来引用数组中每个元素的编号。 $ vim test25.sh 输入代码: #!/bin/bash arr=(12 22 32) arr[0]=10 echo ${arr[0]} 运行代码: $ bash test25.sh 10   四、尖括号 < 和 > 重定向 test.sh > filename:重定向test.sh的输出文件 filename 中。如果 filename 存在的话,那么将会被覆盖。 test.sh &> filename:重定向 test.sh 的 stdout(标准输出)和 stderr(标准错误)到 filename 中。 test.sh >&2:重定向 test.sh 的 stdout 到 stderr 中。 test.sh >> filename:把 test.sh 的输出追加到文件 filename 中。如果filename 不存在的话,将会被创建。   nohup spark-sql --master yarn --queue $queue --executor-memory 40G --driver-memory 10G --num-executors 100 --executor-cores 16 -hiveconf yesterday=$yestertoday -f update-table.hql > log/$yestertoday-run-update.log 2>&1 & 可以修改为 nohup spark-sql --master yarn --queue $queue --executor-memory 40G --driver-memory 10G --num-executors 100 --executor-cores 16 -hiveconf yesterday=$yestertoday -f update-table.hql &> log/$yestertoday-run-update.log & nohup & 放到后台运行   对于&1 更准确的说应该是文件描述符 1,而1标识标准输出,stdout。 对于2 ,表示标准错误,stderr。 2>&1 的意思就是将标准错误重定向到标准输出。这里标准输出已经重定向到了 log/$yestertoday-run-update.log。那么标准错误也会输出到/ log/$yestertoday-run-update.log 五、竖线 | 管道 分析前边命令的输出,并将输出作为后边命令的输入。这是一种产生命令链的好方法。 $ vim test26.sh 输入代码: #!/bin/bash tr 'a-z' 'A-Z' #小写字母替换成为大写字母 exit 0 现在让我们输送ls -l的输出一个脚本中: $ ls -l | sh test26.sh # ls -l输出当前文件夹下的所有文件和问价夹(详细信息) 等价于 ll = ll | sh test26.sh 输出内容均变为了大写字母。   六、破折号 - 1.选项,前缀 -eq 等于 -ne 不等于 -gt 大于 -ge 大于等于 -lt 小于 -le 小于等于 在所有的命令内如果想使用选项参数的话,前边都要加上“-”。   2.用于重定向stdin或stdout 七、波浪号(~) ~ 表示 home 目录      

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐