shell 入门(一)

1、编写第一个shell脚本

#!/bin/bash                #这里用来声明脚本解释器,一般写成#!/usr/bin/env bash。
#this is my first shell    #这一句前面有个“#”号,没有实际意思,相当于被注释掉了
echo "Welcome to netlab"   #这句就是输出“"””内的内容,即Welcome to netlab

执行脚本的方式有两种:

  1. 第一种是赋予脚本执行的权限,一般是755权限[r(4)是读取的权限,w(2)是写的权限,x(1)是执行的权限。]
  2. 第二种是/bin/bash First_shell.sh
root@cat:~/shell# chmod 755 Firstshell.sh
root@cat:~/shell# ./Firstshell.sh
Welcome to netlab
root@cat:~/shell# /bin/bash Firstshell.sh
Welcome to netlab

2、shell的变量命名规则

  1. 首字符必须为字母(A-Z,a-z)
  2. 中间不能有空格,可以使用下划线
  3. 不能使用标点符号
  4. 不能使用bash里的关键字(可以输入help查看)
  5. 例子如下:
    #!/bin/bash
    
    A=123            #正确
    a_123=123        #正确
    #_123 = 123      #错误
    
    echo "$A"
    echo "$a_123"
    #echo "$_123"
    root@cat:~/shell# ./Secondshell.sh
    123
    123
    

    如果第三个变量没有被注释的的,会出现如下报错,首先修改代码:

#!/bin/bash

A=123
a_123=123
_123 = 123

echo "$A"
echo "$a_123"
echo "$_123"
root@cat:~/shell# ./Secondshell.sh
./Secondshell.sh: line 5: _123: command not found     #这是报错信息
123
123

3、变量的引用

引用变量用“$”

4、Shell常见的系统变量解析

  1. $0 :当前程序的名称。
  2. $n :当前程序第n个参数 n=1,2,3………。
  3. $* :当前的所有的参数(不包括程序本身)。
  4. $# :当前程序参数的个数(不包括程序本身)。
  5. $? :命令执行完后的状态,一般返回0表示成功。
  6. $UID  :当前用户的id。
  7. $PWD :当前所在的目录。

5、关系运算符

shell 入门(一)_第1张图片

6、字符串运算符

shell 入门(一)_第2张图片

7、文件测试运算符 

shell 入门(一)_第3张图片

8、if 语句的语法

if (表达式);then
   
    语句1

else

    语句2

fi
#if后面跟的是“((    ))”或者“[[  ]]”则一般用于判断数字大小,比较的。

#if后面跟的是 “[  ]”则一般用于判断文件运算符的

9、例子,测试数字大小

代码如下:

#!/bin/bash
num=10
if (($num > 4));then
echo "This is num is $num greater than 4!"
else
echo "This is num is $num less than 4!"
fi

控制台输出如下:

root@cat:~/shell# ./Forthshell.sh
This is num is 10 greater than 4!

10、例子,判断文件是否存在

代码如下:

#!/bin/bash
file="/root/shell/test.txt"
if [ -e $file ];then
        echo "$file exist"
else
        touch $file
fi

控制台输出如下:

root@cat:~/shell# ls -l                          #现在查看并没有这个文件
total 20
-rwxr-xr-x 1 root root 103 Jun 19 15:38 Fifthshell.sh
-rwxr-xr-x 1 root root  61 Jun 19 14:55 Firstshell.sh
-rwxr-xr-x 1 root root 131 Jun 19 15:33 Forthshell.sh
-rwxr-xr-x 1 root root  78 Jun 19 15:20 Secondshell.sh
-rwxr-xr-x 1 root root 504 Jun 19 15:07 Thirdshell.sh
root@cat:~/shell# ./Fifthshell.sh                #执行脚本
root@cat:~/shell# ls -l                          #没有文件的时候执行创建操作,所以文件被创建了
total 20
-rwxr-xr-x 1 root root 103 Jun 19 15:38 Fifthshell.sh
-rwxr-xr-x 1 root root  61 Jun 19 14:55 Firstshell.sh
-rwxr-xr-x 1 root root 131 Jun 19 15:33 Forthshell.sh
-rwxr-xr-x 1 root root  78 Jun 19 15:20 Secondshell.sh
-rw-r--r-- 1 root root   0 Jun 19 15:39 test.txt
-rwxr-xr-x 1 root root 504 Jun 19 15:07 Thirdshell.sh

11、循环if-else语句

代码如下

#!/bin/bash
score=80
if [[ $score -gt 80 ]];then
        echo "very good"
elif [[ $score -gt 70 ]];then
        echo "good"
elif [[ $score -gt 60]];then
        echo "pass"
else
        echo "Not pass!"
fi

控制台输出如下

root@cat:~/shell# ./Sixthshell.sh
good

需要注意的事情是:shell里面的空格使用很严格,空格作为分隔符,如果没有进行合适的分割的话,程序会出一些问题,难受

仅仅记录平时的学习,如果侵权,联系删除

你可能感兴趣的:(Linux,Shell)