linux脚本


1 理论
2 语法
2.1 & 和 && ,| 和 || ,&> 和 > 的区别

& 表示任务在后台执行,如要在后台运行

[root@localhost opt]java -jar ***.jar &

&& 表示前一条命令执行成功时,才执行后一条命令

[root@localhost opt]ls -l && cd activemq
drwxr-x---. 12 root  root   235 11月  6 21:46 activemq
drwxr-xr-x.  2 root  root     6 11月 21 22:45 data-controll
drwxr-xr-x.  3 root  root    28 11月  8 05:39 dbdata
drwxr-x---.  5 root  root    81 11月 14 19:06 disconf
drwxr-x---.  8 es    es     202 11月 21 22:21 elasticsearch
-rwxr-xr-x.  1 root  root  2164 10月 12 2017 enable_port.sh
drwxr-xr-x.  3 root  root    36 11月 21 22:02 es
drwxr-xr-x.  8 root  root   275 10月 21 19:19 jdk
drwxr-x---.  7 root  root   117 10月 21 23:25 kafka
drwxr-x---.  4 root  root   250 11月 25 23:41 mongodb
drwxr-x---. 10 mysql mysql  206 11月 14 18:46 mysql
drwxr-x---. 14 nginx nginx  242 10月 21 23:25 nginx
drwxr-xr-x.  3 root  root    71 10月 21 19:05 omp
drwxr-x---.  2 root  root     6 10月 21 19:06 omp-repos-tmp
drwx------.  6 root  root  4096 12月 11 22:12 redis
drwxr-xr-x.  8 root  root    86 11月  8 05:39 uyun
drwxr-xr-x.  2 root  root    78 11月 21 18:50 zhoucg
drwxr-xr-x.  6 root  root   188 11月 21 21:58 zhoucgwl
drwxr-x---. 12 root  root  4096 12月 11 22:12 zookeeper

| 表示管道,上一条命令的输出,作为下一条命令参数(输入)

[root@localhost opt]ps -ef | grep nginx
root      3690     1  0 22:12 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
root      3692  3690  0 22:12 ?        00:00:00 nginx: worker process
root     18709  1990  0 22:26 pts/0    00:00:00 grep --color=auto nginx

|| 表示上一条命令执行失败后,才执行下一条命令,

asx -l || cd ..
-bash: asx: 未找到命令 会执行cd ..命令

对于>符号是指:将正常信息重定向

如 find / -name "*.txt" > /tmp/log.txt
在跟目录下根据名字来查找*.tx输入的日志放置/tmp/log.txt文件中

&>可以将错误信息或者普通信息都重定向输出

[root@localhost opt]# firewall-cmd --state &> /opt/zhoucg.txt
表示将 firewall-cmd --state命令的输出结果重定向输出到 /opt/zhoucg.txt 文件中
2.2 /dev/null

/dev/null:表示 的是一个黑洞,通常用于丢弃不需要的数据输出, 或者用于输入流的空文件

[root@localhost opt]# firewall-cmd --state &> /dev/null
表示将对应的结果输出到 /dev/null 中,直接丢弃
2.3 2>/dev/null 和 >/dev/null 2>&1 和 2>&1>/dev/null 的区别

2> /dev/null 意思就是把错误输出到“黑洞中”

[root@localhost opt]# asdf -l 2> /opt/zhoucg.txt
上面语句的意思是会将 asdf -l 这个错误的命令结果:-bash: asdf: 未找到命令 输出到 /opt/zhoucg.txt目录中

> /dev/null 2> &1
默认情况是1,也就是等同于: 1> /dev/null 2> & 1,意思就是把标准输出重定向到"黑洞",还把 输出2 重定向到标准输出1,也就是标准输出和错误都进入了"黑洞"

2>&1 >/dev/null
意思就是把错误输出2重定向到标准输出1,也就是屏幕,标准输出进了“黑洞”,也就是标准输出进了黑洞,错误输出打印到屏幕

[root@localhost opt]# ls zhoucg 2>&1  > /opt/zhoucg.txt

文件描述符
Linux系统预留可三个文件描述符:0、1和2,他们的意义如下所示:
0——标准输入(stdin)
1——标准输出(stdout)
2——标准错误(stderr)

2.4 $ (()), $() ,` `反引号,${}的区别

$() ,` ` 这两个都能达到相同的效果,都能够达到命令替换的效果,在操作上,这两者都是达到相应的效果,但是建议使用$( ),理由如下:

``很容易与’'搞混乱,尤其对初学者来说,而$( )比较直观。
最后,$( )的弊端是,并不是所有的类unix系统都支持这种方式,但反引号是肯定支持的。

[root@local opt]param_one=$(echo 1}
[root@local opt]echo current param_one is ${param_one}

${ }变量替换 、 ${} 里面还可以有 #,##,#*,##,% ,%%

2.5 shell脚本的参数传递方式

1,$0 $1,$2
采用$0,$1,$2…等方式获取脚本命令行传入的参数,值得注意的是, 0 获 取 到 的 是 脚 本 路 径 以 及 脚 本 名 , 后 面 按 顺 序 获 取 参 数 , 当 参 数 超 过 10 个 时 ( 包 括 10 个 ) , 需 要 使 用 0获取到的是脚本路径以及脚本名,后面按顺序获取参数,当参数超过10个时(包括10个),需要使用 010(10)使{10},${11}…才能获取到参数,但是一般很少会超过10个参数的情况。

2,getopts获取

getopts option_string variable
第一个参数是-a这样的选项,第二个参数是hello这样的参数
选项之间可以通过冒号:进行分割,也可以直接连接,: 表示选线后面必须带有参数,如果没有可以不加实际值进行传递
例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。
while getopts "a:b:cdef" opt; do
  case $opt in
    a)
      echo "this is -a the arg is ! $OPTARG" 
      ;;
    b)
      echo "this is -b the arg is ! $OPTARG" 
      ;;
    c)
      echo "this is -c the arg is ! $OPTARG" 
      ;;
	ab)
	  echo "this is -ab the arg is ! $OPTAGE"
	  ;;
    \?)
      echo "Invalid option: -$OPTARG" 
      ;;
  esac
done



[hello@Git shell]$ bash test.sh -a hello
this is -a the arg is ! hello
[hello@Git shell]$ bash test.sh -a
test.sh: option requires an argument -- a
Invalid option: -
2.6 case条件判断
#!/bin/bash
case "$1" in  
   'start_foreground')  
      echo "go to start_foreground"  
      ;;
   'debug')
      echo "go to debu"  
      ;;
   'start')  
      echo "go to start"  
      ;;  
   'stop')  
     echo "go to stop"   
     ;;  
   'restart')  
     echo "go to restart"  
     ;;  
   'status')  
     echo "go to status"  
     ;;  
   'info')  
     echo "go to info"  
     ;;  
  *)
echo "Usage: $0 {start_foreground|start|stop|restart|status|info}"   
esac   
2.7 exit code

exit 0:正常运行程序并退出程序;
exit 1:非正常运行导致退出程序;

2.8 sed命令

命令

sed [options] commands [inputfile...]

替换原始的值为新的值的方式

sed -i 's#'$APP_OLD_ERR_PATH'#'$APP_NEW_ERR_PATH'#g' $CONF_FI

sed -i 就是直接对文本文件进行操作

sed -i 's/原字符串/新字符串/' /home/1.txt
sed -i 's/原字符串/新字符串/g' /home/1.txt
2.9 文件测试运算符

在这里插入图片描述

[待更新]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值