Linux的Shell编程入门

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_38930706/article/details/90722057

WHAT IS SHELL?

      Shell 是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动、挂起、停止甚至是编写一些程序。

      Shell是一个功能强大的语言,易编写、易调试、灵活性强。是解释执行的脚本语言,在Shell中可以调用Linux命令。

HELLOWORLD!

     Shell文件的后缀是   .sh 。我们直接创建helloworld.sh  编写如下代码并保存:

#!/bin/bash
echo "helloworld"

     怎么执行呢?直接运行helloworld.sh 文件啊,使用相对路径 ./helloworld.sh 和绝对路径都是OK的,也可以使用如下的命令组合啊,但需要注意的是,我们创建的文件是没有操作权限的,暴力给一下“chmod 777 helloworld.sh”,再执行:

sh ./helloworld.sh
bash ./hellworld.sh

那种路径你自己选啊,咋舒服咋来。

Shell中的变量

      shell中的变量分为系统变量和用户自定义变量,可以通过set指令查看所有的系统变量,set指令会以“指令名=值的形式列出所有系统变量”,下列指令展示查看部分系统变量:

[root@hadoop0522 helloworld]# echo $HOME
/root
[root@hadoop0522 helloworld]# cd ~
[root@hadoop0522 ~]# echo $HOME
/root
[root@hadoop0522 ~]# echo $PWD
/root
[root@hadoop0522 ~]# cd /opt/helloworld/
[root@hadoop0522 helloworld]# echo $PWD
/opt/helloworld
[root@hadoop0522 helloworld]# echo $SHELL
/bin/bash
[root@hadoop0522 helloworld]# echo $USER
root

      Shell中定义变量无数据类型,使用下面方式定义、撤销、定义静态变量:

[root@hadoop0522 helloworld]# ALOHA=zhangyifei
[root@hadoop0522 helloworld]# echo $ALOHA
zhangyifei
[root@hadoop0522 helloworld]# unset ALOHA
[root@hadoop0522 helloworld]# echo $ALOHA

[root@hadoop0522 helloworld]# readonly ALOHA=199651
[root@hadoop0522 helloworld]# echo $ALOHA
199651
[root@hadoop0522 helloworld]# unset ALOHA
-bash: unset: ALOHA: cannot unset: readonly variable

可以看到,静态变量甚至无法删除,那么怎么删除静态变量呢?参考重启Shell。

PS:在定义变量时注意:需要遵循定义的规范(字母数字下划线);等号两侧不能有空格;变量名大写;单引号将所有特殊字符脱意,双引号仅将特殊字符脱意。脱意这个我没验证成功~暂且无视,用 echo -e 不脱意~

      反引号(一般是波浪线那个键)方式定义的变量是可执行的指令:

[root@hadoop0522 helloworld]# Q=`ls -la`
[root@hadoop0522 helloworld]# echo $Q
total 12 drwxr-xr-x. 2 root root 4096 May 31 20:18 . drwxr-xr-x. 4 root root 4096 May 31 20:06 .. -rwxrwxrwx. 1 root root 30 May 31 20:07 helloworld.sh

可见,ls -la 已经执行了。也可以这样定义: Q=$(ls -la)效果相同。

        操作参数

编写 param.sh文件,如下:

#!/bin/bash 

echo "$1"
echo "$3"
echo "$*"
echo "$@"
echo "$#"

$1:获取第一个参数;$3:获取第三个参数;$*:获取所有参数,所有参数看成一个整体;$@:获取所有参数,每个参数分开对待;$#:获取参数的个数。

执行param.sh文件并传递5个参数:1 2 3 4 5 

[root@hadoop0522 helloworld]# ./param.sh 1 2 3 4 5
1
3
1 2 3 4 5
1 2 3 4 5
5

另外还有$$:获取当前进程号;$!:获取后台最后一个进程的进程号;$?:最后一次执行命令的返回状态,0表示执行正确,非0错误。

运算

格式:$((算式)) 或 $[算式] ; expr m+n,不能定义变量 ; 

运算符: +   -   /*(乘) /  %。

怎样计算一个二加三乘四?

expr `expr 2 + 3` \* 4

条件判断

语法:[ 判断内容 ]  判断内容和括号间需要跟空格,正确返回true,错误或无判断逻辑返回false。

= -lt -le -gt -ge -eq -ne -r -w -x -f -e -d
字符串比较 小于 小于等于 大于 大于等于 等于 不等于 读权限 写权限 操作权限 文件存在且常规 文件存在 文件存在且是目录

一个if判断语句:

if [ 判断条件 ] 
 then 
 这里是执行的代码
elif [ 判断条件 ]
 then
 这里是执行的代码
fi

选择语句:

    直接上case,输入一个参数,是1打印1,是2打印2,其他打印other:

#!/bin/bash

case $1 in 
"1")
echo "1"
;;
"2")
echo "2"
;;
*)
echo "other"
;;
esac

结果:

循环语句:

     直接上case:

#!/bin/bash
#for in bianli
for i in "$*"
do
        echo "$i"
done

for j in "$@"
do
        echo "$j"
done

输入及输出结果:

   使用for循环求和1+2+...+100:

#!/bin/bash

s=0
for((i=1;i<=100;i++))
do
        s=$[$s+$i]
done

echo $s

结果:

使用while循环计算1+2+...+100:

#!/bin/bash

s=0
i=0

while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done

echo $s

打印结果:

函数

直接上demo,输入两个数字并求和:

#!/bin/bash

function sum()
{
        s=0
        s=$[$1 +$2]
        echo "$s"
}

read -p "please input the number1:" n1;
read -p "please input the number2:" n2;
sum $n1 $n2;

结果:

 

猜你喜欢

转载自blog.csdn.net/weixin_38930706/article/details/90722057