shell 脚本简单入门

好久不写shell脚本,有些生疏。总结下shell的语法,以便后续参考,快速捡起来。

  1. shell 脚本执行的3种方式:
    1). ./xx.sh (xx.sh 需要有执行权限)
    2). source xx.sh
    3). bash xx.sh

  2. 变量定义
    var=2 //注意:=左右没有空格

  3. 变量引用的2种方式
    $符号就是变量解引用符号

       $var    
       ${var}
    
  4. 字符串初始化

       string=hello world
       string="hello world"
    
  5. 单引号、双引号
    单引号中:完全字面替换(不可包含单引号本身)

          1 #!/bin/bash
          2 
          3 string="hello world"
          4 `echo '$string\\'
    

    `
    运行:

    ricky@ubuntu:~/code/shell$ ./1.sh
    $string\\
    
     双引号中:转义字符/
     $加变量名可以取变量的值
     反引号仍表示命令替换
     \$表示$的字面值输出$符号
     \`表示`的字面值
     \"表示"的字面值
     \\表示\的字面值
    
       1 #!/bin/bash
       2 string="hello world"
       3 echo "$string\\"
    

    运行:

    ricky@ubuntu:~/code/shell$ ./1.sh
    hello world\
    
  6. 调用linux 命令
    1)直接执行 pwd

  1. 用反引号(和~位于同一个按键)括起来
```
    `pwd`
```
  1. 分支
    注意[ 表达式 ]里面表达式前后各有一个空格
    if [ 表达式 ]; then
    xxx
    else
    xxx
    fi

    1).判断文件是否存在 -f

        if [ -f 1.sh ]; then
            	echo "file exist"
    	else
    	        echo "file does not exist"
    	fi
    

    2). 判断目录是否存在 -d

    	if [ -d `pwd` ]; then
            	echo "`pwd` exist"
    	else
            	echo "dir does not exist"
    	fi
    
     //输出:/home/ricky/code/shell exist
    

    3). 判断字符串是否相等: str1 = str2 一个等号,和C语言不一样

          1 #!/bin/bash
          2 string="hello world"
          3 
          4 if [ "$string" = "hello world" ]; then
          5     echo "the same string"
          6 else
          7     echo "dif string"
          8 fi
    

    4). 数字比较
    相等(-eq)
    大于(-gt)
    小于(-lt)
    大于等于(-ge)
    小于等于(-le)

        1 #!/bin/bash
          2 
          3 n=10
          4 
          5 if [ $n -gt 5 ]; then
          6     echo "n is grate than 5"
          7 fi
    

    5). 判断字符串是否为空(-z)

          1 #!/bin/bash
          2 
          3 string="hello"
          4 
          5 if [ -z $string ]; then
          6     echo "null string"
          7 else
          8     echo "no null string"
          9 fi
    

    6). 逻辑或(-o) 逻辑与(-a)

          1 #!/bin/bash
          2 
          3 string="hello"
          4 n=10
          5 
          6 if [ -z $string -o $n -gt 5 ]; then
          7     echo "null string or n is greate than 5"
          8 else
          9     echo "no null string"
         10 fi
    

    7). if…else…快速写法:用&&或者||表示

    if [ A ]; then
    do B
    fi
    可以用 [ A ] && do B 表示

       1 #!/bin/bash
       2 
       3 n=10
       4 
       5 if [ $n -gt 5 ]; then
       6     echo "n is great than 5"
       7 fi
       8 
       9 [ $n -gt 5 ] && echo "n is great than 5"
    
  运行
ricky@ubuntu:~/code/shell$ ./1.sh
n is great than 5
n is great than 5
  if [ A ]; then
  	do B
  fi
 可以用 [ ! A ] || do B 表示
  ```
    1 #!/bin/bash
    2 
    3 n=10
    4 
    5 if [ $n -gt 5 ]; then
    6     echo "n is great than 5"
    7 fi
    8 
    9 [ ! $n -gt 5 ] || echo "n is great than 5"

运行:

 	ricky@ubuntu:~/code/shell$ ./1.sh
  n is great than 5
  n is great than 5

&& 和 || 还可以执行命令
cmd1 && cmd2 :
若cmd1执行完毕且正确执行($?=0),则开始执行cmd2
若cmd1执行完毕且返回出错($?≠0),则不执行cmd2
1 #!/bin/bash 2 3 pwd && ls
运行:
ricky@ubuntu:~/code/shell$ ./1.sh /home/ricky/code/shell 1.sh

   1 #!/bin/bash
   2 
   3 ! pwd && ls
 ```
 运行:
 ```		 
 	ricky@ubuntu:~/code/shell$ ./1.sh
 	/home/ricky/code/shell
 ```
 cmd1 || cmd2		
 若cmd1执行完毕且正确执行(`$?=0`),则不执行cmd2
 若cmd1执行完毕且返回出错(`$?≠0`),则执行cmd2

  1 #!/bin/bash
  2 
  3 ! pwd || ls
	运行:

		   	ricky@ubuntu:~/code/shell$ ./1.sh
			/home/ricky/code/shell
			1.sh
8.  for 循环
      
	```
	      1 #!/bin/bash
	      2 
	      3 for i in 1 2 3 4
	      4 do
	      5     echo "$i"
	      6 done
	```

 
	输出:
	1
	2
	3
	4

9.  while 循环

	```
	      1 #!/bin/bash
	      2 
	      3 i=0
	      4 while [ $i -le 4 ]
	      5 do
	      6     echo "$i"
	      7     let i++
	      8 done
	```

	输出:
	0
	1
	2
	3
	4	

10. switch case

	```
	      1 #!/bin/bash
	      2 
	      3 cond="in"
	      4 
	      5 case $cond in
	      6 in) echo "$cond" ;;
	      7 out) echo "out"  ;;
	      8 esac
	```

	//注意,和C语言不同,没有break

11. 注释多行(:<<!和!)
		:<<!
			line1
			line2
			...
		!

12. shell程序的参数:

	```
		$0: shell 脚本的名字
	        $1 $2 $3: 第1/2/3参数 
		$#: 参数的个数(不包含脚本的名字)
	        $@: 所有的参数
	```

	```
	      1 #!/bin/bash
	      2 
	      3 echo "para_0: $0"
	      4 echo "para_1: $1"
	      5 echo "para_2: $2"
	      6 echo "para num: $#"
	      7 echo "para list: $@"
	```

	1)
	运行: 

	```
	ricky@ubuntu:~/code/shell$ ./1.sh
	para_0: ./1.sh
	para_1: 
	para_2: 
	para num: 0
	para list:
	```

	2)
	运行:

	```
	ricky@ubuntu:~/code/shell$ ./1.sh a b c
	para_0: ./1.sh
	para_1: a
	para_2: b
	para num: 3
	para list: a b c
	```

15. shift 指令
shift可以用来向左移动位置参数

	```
	      1 #!/bin/bash
	      2 
	      3 while [ $# -gt 0 ]
	      4 do
	      5     echo $@
	      6     shift  #shift para rightward
	      7 done
	```

	运行:
	
	```
	ricky@ubuntu:~/code/shell$ ./1.sh a b c
	a b c
	b c
	c
	```











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值