【Linux】shell编程(一) 变量

  • 【Linux】shell编程(一) 变量已关闭评论
  • 132 次浏览
  • A+
所属分类:linux技术
摘要

简单的命令可以在命令行中直接输入,但是复杂的命令需要写在脚本里。例如一个简单的shell脚本:


【Linux基础】shell编程(一) 变量

什么是shell编程

简单的命令可以在命令行中直接输入,但是复杂的命令需要写在脚本里。例如一个简单的shell脚本:

#!/bin/bash  #输出一行 echo "Hello World!" 

#开始的行是注释行,空行会被忽略。

如何运行shell脚本

  • 方式一:直接输入脚本的相对路径或绝对路径

    ./run.sh 
    • 需要给shell脚本添加可执行权限,否则会报错:Permission denied
  • 方式二:sh+脚本的相对路径或绝对路径

    sh ./run.sh 
    • 不需要添加可执行权限

第一行 #!/bin/bash

第一行叫什么?

WHAT IS THIS LINE CALLED?

This first line (#!/bin/bash or #!/bin/sh) has a name. It is known as ‘she-bang‘(shabang). This derives from the concatenation of the tokens sharp (#) and bang (!). It is also called as sh-bang, hashbang, poundbang or hash-pling. In computing, a she-bang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.

shabang,由sharp(#)和bang(!)组合而来,必须位于一个脚本的第一行

为什么要加这个,有什么用?

In Unix-like Operating Systems when a script starting with a she-bang(#!) is executed as a program, the program loader parses the rest of the script’s initial line as a interpreter-directive. Thus the specified interpreter program is run instead, passing to it as an argument the path that was used initially through the script.

In simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated. Unix-like operating systems has variety of shells and each of script header lines call a different command interpreter.

如果shell看到第一行是shabang,shell就知道这个文件是一个shell脚本,并按照shabang的指引到/bin/bash找到指定的shell解释器,然后把文件中的命令传给shell。

解释器可以是bash,也可以是csh等等。

SOME she-bang EXAMPLES

*#!/bin/sh :Executes the script using the Bourne shell or a compatible shell, with path /bin/sh*

*#!/bin/bash :Executes the script using the Bash shell.*

*#!/bin/csh -f :Executes the script using C shell or a compatible shell.*

*#!/usr/bin/perl -T :Executes the script using perl with the option of taint checks*

*#!/usr/bin/env python :Executes the script using python by looking up the path to the python interpreter automatically from the environment variables*

shell的变量

变量的赋值和使用

#!/bin/bash  #将一个字符串赋给变量A LOG="monday" echo "The value of logfile is:"  #美元符号用于变量替换 echo $LOG 

运行结果:

$ sh ./variable.sh The value of logfile is: monday 
  • 变量的赋值:

    • 变量赋值时,等号两边都不能打空格
    • 变量名可以由字母、数字和下划线组成,但是不能以数字开头
    • 变量名称一般为大写(代码规范,不是语法要求)
  • 变量的使用:

    • 当需要使用变量时,要$对变量进行替换。BASH中,美元符号$用于对一个变量进行解析,shell在碰到$引导的变量时,会自动将其换成这个变量的值。
  • 变量作用范围

    • 变量只在其所在脚本有效。

    • source可以强行让一个脚本影响其父环境

      • $ source variable.sh The value of logfile is: monday $ echo $LOG monday 
    • 与之相反,export可以让脚本影响其子shell环境

      • $ export count=5		 ##输出变量count $ bash				##启动子shell $ echo $count 5 $ exit				##回到先前的shell中 exit 
    • 使用unset可以注销变量

      • unset log 

变量替换

  • $用于解析变量,如果要输出这个符号,需要使用转义字符''

    • $ LOG='Monday' $ echo 
  • shell提供了花括号"{}"来限定一个变量的开始和结束。当需要紧跟变量输出字母后缀时,必须使用这个功能

    • $ WORD='big' $ echo "This apple is ${WORD}ger" This apple is bigger 

位置变量

可以向shell脚本传命令行参数,shell脚本中使用以数字命名的变量来存放这些参数,称为位置变量。

  • 简单地说,第一个位置变量存放在$1,第二个存放在$2,以此类推。当变量数量超过10个时,需要加花括号把数字括起来。例如${10}${23}等。

  • $0用于存放脚本自己的名字。

!#/bin/bash  echo "$0 = *$0*" echo "$1 = *$1*" echo "$2 = *$2*" echo "$3 = *$3*" 

运行结果:

$ sh ./diaplay_para.sh first second $0 = *display_para.sh* $1 = *firsh* $2 = *second* $3 = **				##不存在第三个变量,所以为空 

除了以数字命名的变量外,shell还提供了另外三个位置变量:

  • $*:包含参数列表
  • $@:包含参数列表,同上
  • $#:包含参数的个数
#!/bin/bash  #显示有多少个参数需要列出 echo "The number of parameter is $#"  for para in $@ do 	echo $para			##也可以写成 echo "$para" done 

运行结果:

$ sh ./list_para.sh first second The number of parameter is 2 first second 

BASH引号规则

shell脚本中可以使用的引号有以下三种:

  • 双引号:阻止Shell对大多数特殊字符(例如#)进行解释。但$`"仍然保持其特殊含义
  • 单引号:阻止Shell对所有字符进行解释
  • 倒引号:`,这个符号通常位于键盘Esc键的下方。当用倒引号括起一个Shell命令时,命令会被执行,并将执行后的结果作为表达式的值。
#!/bin/bash  LOG=Saturday  echo "Today is $LOG" echo 'Today is $LOG'  echo "Today is `date`" echo 'Today is `date`' 

运行结果:

Today is Saturday Today is $LOG  Today is Thu Jun  8 17:37:43 CST 2023 Today is `date` 

小结

  • 运行Shell脚本:sh+脚本的相对路径或绝对路径。
  • 第一行的"#!/bin/bash"是shabang(sharp bang),表明Shell解释器的路径。有Shabang的文件运行时会被自动识别成Shell脚本。
  • 变量赋值时,等号两边不能有空格。
  • $符号后面的变量会被自动替换成变量的值。
  • 数字命名的变量表示传入的位置变量,如($1), (${12})($@)($*)表示位置变量列表,($#)表示位置变量的数量。
  • 双引号阻止大多数字符解析,单引号阻止所有字符解析,倒引号执行命令并作为表达式的值。