翼度科技»论坛 云主机 LINUX 查看内容

【Linux】shell编程(一) 变量

8

主题

8

帖子

24

积分

新手上路

Rank: 1

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


目录

什么是shell编程

简单的命令可以在命令行中直接输入,但是复杂的命令需要写在脚本里。例如一个简单的shell脚本:
  1. #!/bin/bash
  2. #输出一行
  3. echo "Hello World!"
复制代码
#开始的行是注释行,空行会被忽略。
如何运行shell脚本


  • 方式一:直接输入脚本的相对路径或绝对路径
    1. ./run.sh
    复制代码

    • 需要给shell脚本添加可执行权限,否则会报错:Permission denied

  • 方式二:sh+脚本的相对路径或绝对路径
    1. 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的变量

变量的赋值和使用
  1. #!/bin/bash
  2. #将一个字符串赋给变量A
  3. LOG="monday"
  4. echo "The value of logfile is:"
  5. #美元符号用于变量替换
  6. echo $LOG
复制代码
运行结果:
  1. $ sh ./variable.sh
  2. The value of logfile is:
  3. monday
复制代码

  • 变量的赋值:

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

  • 变量的使用:

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

  • 变量作用范围

    • 变量只在其所在脚本有效。
    • source可以强行让一个脚本影响其父环境

        1. $ source variable.sh
        2. The value of logfile is:
        3. monday
        4. $ echo $LOG
        5. monday
        复制代码

    • 与之相反,export可以让脚本影响其子shell环境

        1. $ export count=5                 ##输出变量count
        2. $ bash                                ##启动子shell
        3. $ echo $count
        4. 5
        5. $ exit                                ##回到先前的shell中
        6. exit
        复制代码

    • 使用unset可以注销变量

        1. unset log
        复制代码


变量替换


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

      1. $ LOG='Monday'
      2. $ echo
      复制代码

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

      1. $ WORD='big'
      2. $ echo "This apple is ${WORD}ger"
      3. This apple is bigger
      复制代码

位置变量

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

  • 简单地说,第一个位置变量存放在$1,第二个存放在$2,以此类推。当变量数量超过10个时,需要加花括号把数字括起来。例如${10},${23}等。
  • $0用于存放脚本自己的名字。
  1. !#/bin/bash
  2. echo "\$0 = *$0*"
  3. echo "\$1 = *$1*"
  4. echo "\$2 = *$2*"
  5. echo "\$3 = *$3*"
复制代码
运行结果:
  1. $ sh ./diaplay_para.sh first second
  2. $0 = *display_para.sh*
  3. $1 = *firsh*
  4. $2 = *second*
  5. $3 = **                                ##不存在第三个变量,所以为空
复制代码
除了以数字命名的变量外,shell还提供了另外三个位置变量:

  • $*:包含参数列表
  • $@:包含参数列表,同上
  • $#:包含参数的个数
  1. #!/bin/bash
  2. #显示有多少个参数需要列出
  3. echo "The number of parameter is $#"
  4. for para in $@
  5. do
  6.         echo $para                        ##也可以写成 echo "$para"
  7. done
复制代码
运行结果:
  1. $ sh ./list_para.sh first second
  2. The number of parameter is 2
  3. first
  4. second
复制代码
BASH引号规则

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

  • 双引号:阻止Shell对大多数特殊字符(例如#)进行解释。但$、` 和"仍然保持其特殊含义
  • 单引号:阻止Shell对所有字符进行解释
  • 倒引号:`,这个符号通常位于键盘Esc键的下方。当用倒引号括起一个Shell命令时,命令会被执行,并将执行后的结果作为表达式的值。
  1. #!/bin/bash
  2. LOG=Saturday
  3. echo "Today is $LOG"
  4. echo 'Today is $LOG'
  5. echo "Today is `date`"
  6. echo 'Today is `date`'
复制代码
运行结果:
  1. Today is Saturday
  2. Today is $LOG
  3. Today is Thu Jun  8 17:37:43 CST 2023
  4. Today is `date`
复制代码
小结


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

来源:https://www.cnblogs.com/zihaojun/p/17467288.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具