shell 教程一:变量,字符串,传参

一,hello shell
   
   
  1. vi hello.sh
写如下内容:
    
    
  1. linux@ubuntu:~/test_shell$ cat hello.sh
  2. #!/bin/bash
  3. echo "hello shell!"
注意这时的.sh文件没有执行权限,要更改为可执行状态
     
     
  1. linux@ubuntu:~/test_shell$ ls -l hello.sh
  2. -rw-rw-r-- 1 linux linux 33 Dec 19 01:14 hello.sh
  3. linux@ubuntu:~/test_shell$ chmod +x hello.sh
  4. linux@ubuntu:~/test_shell$ ls -l hello.sh
  5. -rwxrwxr-x 1 linux linux 33 Dec 19 01:14 hello.sh
运行hello.sh
      
      
  1. linux@ubuntu:~/test_shell$ ./hello.sh
  2. hello shell!
练习:
       
       
  1. linux@ubuntu:~/shell_test$ vi shell.sh
  2. linux@ubuntu:~/shell_test$ /bin/sh shell.sh
  3. hello world!
  4. linux@ubuntu:~/shell_test$ ./shell.sh
  5. bash: ./shell.sh: Permission denied
  6. linux@ubuntu:~/shell_test$ chmod +x shell.sh
  7. linux@ubuntu:~/shell_test$ ./shell.sh
  8. hello world!
  9. linux@ubuntu:~/shell_test$ ls -l shell.sh
  10. -rwxrwxr-x 1 linux linux 33 Dec 19 06:56 shell.sh
  11. linux@ubuntu:~/shell_test$ cat shell.sh
  12. #!/bin/bash
  13. echo "hello world!"

二,shell变量
1,定义变量
定义变量时,变量名不加美元符号

注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则:

  • 首个字符必须为字母(a-z,A-Z)。
  • 中间不能有空格,可以使用下划线(_)。
  • 不能使用标点符号。
  • 不能使用bash里的关键字(可用help命令查看保留关键字)。
  • 例如:
  •        
           
    1. linux@ubuntu:~/shell_test$ cat shell.sh
    2. #!/bin/bash
    3. echo "hello world!"
    4. your_name="Liu Jing"
    5. echo $your_name
    6. echo ${your_name}
    输出:
           
           
    1. linux@ubuntu:~/shell_test$ ./shell.sh
    2. hello world!
    3. Liu Jing
    4. Liu Jing
注意:
  1. your_name = "xiao ming"
等号“=”前后都不能有空格,否则不能识别,这点比较坑
变量名外面的花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界
   
   
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo $your_name
  6. echo ${your_name}
  7. for skill in C C++ Java Pthon Andorid; do
  8. echo "I am good at ${skill}Language!"
  9. done
  10. linux@ubuntu:~/shell_test$ ./shell.sh
  11. hello world!
  12. Liu Jing
  13. Liu Jing
  14. I am good at CLanguage!
  15. I am good at C++Language!
  16. I am good at JavaLanguage!
  17. I am good at PthonLanguage!
  18. I am good at AndoridLanguage!

如果不给skill变量加花括号,写成echo "I am good at $skillScript",解释器就会把$skillLanguage!当成一个变量(其值为空),代码执行结果就不是我们期望的样子了。

推荐给所有变量加上花括号,这是个好的编程习惯。

下面证明其为空:

    
    
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo $your_name
  6. echo ${your_name}
  7. for skill in C C++ Java Pthon Andorid; do
  8. echo "I am good at $skillLanguage!"
  9. done
  10. linux@ubuntu:~/shell_test$ ./shell.sh
  11. hello world!
  12. Liu Jing
  13. Liu Jing
  14. I am good at !
  15. I am good at !
  16. I am good at !
  17. I am good at !
  18. I am good at !

2,修改已定义过的变量
已定义的变量,可以被重新定义,
但注意,第二次赋值的时候不能写$your_name="alibaba",使用变量的时候才加美元符($)。
修改如下:
     
     
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo $your_name
  6. your_name="xiao niu"
  7. echo $your_name
  8. linux@ubuntu:~/shell_test$ ./shell.sh
  9. hello world!
  10. Liu Jing
  11. xiao niu
3,只读变量
      
      
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo $your_name
  6. readonly your_name
  7. your_name="xiao niu"
  8. echo $your_name
  9. linux@ubuntu:~/shell_test$ ./shell.sh
  10. hello world!
  11. Liu Jing
  12. ./shell.sh: line 9: your_name: readonly variable
  13. Liu Jing

删除变量

使用 unset 命令可以删除变量。变量被删除后不能再次使用。unset 命令不能删除只读变量。

语法:

     
     
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo $your_name
  6. unset your_name
  7. echo $your_name
  8. echo "end"
  9. linux@ubuntu:~/shell_test$ ./shell.sh
  10. hello world!
  11. Liu Jing
  12. end

Shell 字符串

字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号

单引号

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  • 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。
    
    
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name='Liu Jing'
  5. echo 'This is $your_name'
  6. echo $your_name
  7. echo "end"
  8. linux@ubuntu:~/shell_test$ ./shell.sh
  9. hello world!
  10. This is $your_name
  11. Liu Jing
  12. end

双引号

双引号的优点:

  • 双引号里可以有变量
  • 双引号里可以出现转义字符
     
     
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name="Liu Jing"
  5. echo "I know your name is ${your_name},is not?"
  6. echo "I know your name is \"$your_name\",is not?"
  7. echo $your_name
  8. echo "end"
  9. linux@ubuntu:~/shell_test$ ./shell.sh
  10. hello world!
  11. I know your name is Liu Jing,is not?
  12. I know your name is $your_name,is not?
  13. Liu Jing
  14. end

拼接字符串

      
      
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "hello world!"
  4. your_name='Liu Jing'
  5. all_name="hello, "$your_name" !"
  6. other_name="hello, ${your_name} !"
  7. echo $your_name $all_name $other_name
  8. echo "end"
  9. linux@ubuntu:~/shell_test$ ./shell.sh
  10. hello world!
  11. Liu Jing hello, Liu Jing ! hello, Liu Jing !
  12. end

获取字符串长度

      
      
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. your_name='Liu Jing'
  4. echo ${#your_name}
  5. linux@ubuntu:~/shell_test$ ./shell.sh
  6. 8

提取子字符串

以下实例从字符串第  2 个字符开始截取  4 个字符:
       
       
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. your_name='Liu Jing'
  4. echo ${your_name:1:4}
  5. linux@ubuntu:~/shell_test$ ./shell.sh
  6. iu J

查找子字符串

查找字符 "a或 n" 的位置,注意: 脚本中 "`" 是反引号,而不是单引号 "'",不要看错了哦。

        
        
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. your_name="Liu Jing is a great man!"
  4. echo `expr index "$your_name" an`
  5. linux@ubuntu:~/shell_test$ ./shell.sh
  6. 7


Shell 注释

以"#"开头的行就是注释,会被解释器忽略。

sh里没有多行注释,只能每一行加一个#号。只能像这样:

#--------------------------------------------
# 这是一个注释
# slogan:学的不仅是技术,更是梦想!
#--------------------------------------------
##### 用户配置区 开始 #####
#
#
# 这里可以添加脚本描述信息
# 
#
##### 用户配置区 结束  #####

如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?

每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。

      
      
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. abc(){
  4. your_name=(liujing xiaoniu xiaohu kurong c++)
  5. echo ${your_name[@]}
  6. echo ${your_name[1]} ${your_name[4]}
  7. echo ${#your_name[@]}
  8. echo ${#your_name[1]}
  9. }
  10. my_name[0]=liu
  11. my_name[1]=jing
  12. my_name[10]=haha
  13. echo ${my_name[@]}
  14. echo ${my_name[1]} ${my_name[10]}
  15. echo ${#my_name[*]}
  16. echo ${#my_name[10]}
  17. linux@ubuntu:~/shell_test$ ./shell.sh
  18. liu jing haha
  19. jing haha
  20. 3
  21. 4
可以看到abc()函数内的内容并没有输出

Shell 传递参数

我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为: $nn 代表一个数字,  $0 为执行的文件,$1 为执行脚本的第一个参数,$2 为执行脚本的第二个参数,以此类推……
    
    
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "Shell args test!"
  4. echo "arg0: $0"
  5. echo "arg1: $1"
  6. echo "arg2: $2"
  7. echo "arg3: $3"
  8. linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
  9. Shell args test!
  10. arg0: ./shell.sh
  11. arg1: jing
  12. arg2: love
  13. arg3: shell

另外,还有几个特殊字符用来处理参数:

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
例:

   
   
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "Shell args test!" #Shell args test!
  4. echo "arg2: $2" #arg2: love
  5. echo "arg count: $#" #arg count: 3
  6. echo "$*" #jing love shell
  7. echo "$$" #8003
  8. echo "$!" #空
  9. echo "$@" #jing love shell
  10. echo "$-" #hB
  11. echo "$?" #0
  12. linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
  13. Shell args test!
  14. arg2: love
  15. arg count: 3
  16. jing love shell
  17. 8003
  18. jing love shell
  19. hB
  20. 0

$* 与 $@ 区别:

  • 相同点:都是引用所有参数。
  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)。
实例证明:
   
   
  1. linux@ubuntu:~/shell_test$ cat shell.sh
  2. #!/bin/bash
  3. echo "Shell args test!"
  4. echo "$*"
  5. echo "$@"
  6. echo "---- \$* ----"
  7. for i in "$*"; do
  8. echo $i
  9. done
  10. echo "---- \$@ ----"
  11. for i in "$@"; do
  12. echo $i
  13. done
  14. linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
  15. Shell args test!
  16. jing love shell
  17. jing love shell
  18. ---- $* ----
  19. jing love shell
  20. ---- $@ ----
  21. jing
  22. love
  23. shell







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值