Lunix Shell编程入门

Lunix Shell编程入门

前言

要想玩转类Unix系统,仅仅会基本的常用操作命令还是远远不够的,本文介绍lunix系统下的Shell编程,通过一系列的简单Shell代码示例,一步步入门Shell编程。

一、Shell是什么?

Linux系统被比作花生一样,有壳有核,Shell就是Lunix系统的外壳,通过Shell可以访问操作Lunix内核的功能和服务。
在这里插入图片描述

二、Shell脚本是什么?

Shell脚本(Shell script)就是用Shell编写的脚本程序。Shell通常就是指Shell脚本。

三、Shell的优点是什么?

  1. Lunix的精髓将多个程序(命令)组装成大型程序,而Shell就是组装这些程序的最好粘合剂。
  2. Shell简单、高效、易维护、随写随用
  3. Shell使得Linux越用越简单

四、示例代码带你入门Shell编程

1. hello.sh

# 1. 创建shell文件
vim 1_hello.sh
# 2. 编写shell命令
#!/bin/bash  # shell编程标配,指定以什么方式执行shell命令,一般都是以bash方式
echo -e "hello,world\n" # echo就类似C语言中的prinft函数,用于输出
# 3. 执行hello.sh
chmod u+x hello.sh # 修改hello.sh文件的用户具有执行权限
./hello.sh # 执行shell命令
  • 执行效果如下图
    在这里插入图片描述
    由以上代码可知shell编程的基本步骤如下:
  1. 创建*.sh文件
  2. 在*.sh文件中编写shell命令
  3. 修改*.sh文件具有执行权限
  4. 执行*.sh文件

2. 定义变量

#!/bin/bash
#define var (note: the between variable name and = don't contain blank)
my_name="mekeater"
echo "1. $my_name" # use var 

# define var
my_year="18"
echo "2. ${my_year}" # use var

# only read var
readonly sex
sex="man"
echo "3. ${sex}"

#delete var
unset my_name
echo "4. $my_name"

注意,shell命令对空格非常敏感,新手如果发现shell总是报错,首先考虑是不是不该有空格的地方加了空格。

  • 执行效果
    在这里插入图片描述

3. 定义环境变量

#!/bin/bash
# 1. export a environment variable
export MY_NAME="mekeater" 
# 2. use (source env_var.sh) in consol 
# 3. export a environment variable success. you can test it by (env | grep MY_NAME)
  • 执行效果
    在这里插入图片描述

4. 内置特殊变量

#!/bin/bash
echo "filename: $0"  # 获取当前shell脚本文件名
echo "parameter1: $1"  # 获取shell脚本传入的第1个参数值
echo "parameter2: $2"  # 获取shell脚本传入的第2个参数值
echo "all parameter: $@"  # 获取shell脚本传入的所有参数值
echo "parameter number: $#" # 获取shell脚本传入的参数个数
  • 执行效果
    在这里插入图片描述

5. 算术运算

#!/bin/bash
a=11
b=5

# methond 1
c=`expr $a + $b`
echo "$a + $b = $c"

# methond 2
c=$[a-b]
echo "$a - $b =  $c"
  • 执行效果
    在这里插入图片描述

6. 关系运算

#!/bin/bash
a=6
b=8

# -eq equal; -ne not equal; -gt greater than; -ge greater eaqual;  -lt less than; -le less equal

if [ $a -eq $b ]
then 
echo "$a equal $b"
else
echo "$a not equal $b"
fi

-eq 等于; -ne 不等于; -gt 大于; -ge 大于等于; -lt 小于; -le 小于等于

  • 执行效果
    在这里插入图片描述

7. 逻辑运算

#!/bin/bash

# || == != && 

a=6
b=9
if [[ $a -gt 0 && $b -gt 0 ]]
then echo "a and b great 0"
fi
  • 执行效果
    在这里插入图片描述

8. 文件类型判断

#!/bin/bash

# -d directory; -f file ; -s is file null?; -e exist

file=$0
echo "curren file is $file"

if [ -f $file ]
then echo "normal file"
fi

if [ -e $file ] 
then 
echo "file is exist"
fi

if [ -r $file ]
then 
echo "readable"
fi

-d 是否为文件夹 ;-f 是否为文件; -r -w -x文件是否可读 可写 可执行;-s文件是否为空;-e文件是否存在

  • 执行效果
    在这里插入图片描述

9. 字符串操作

#!/bin/bash
str="hello world"
echo "the string($str) length is ${#str}"
echo "substr ${str:1:3}"
#find string
matched=`expr index "$str" wo`
echo  "the matched index is $matched"
  • 执行效果
    在这里插入图片描述

10. 字符串运算

#!/bin/bash
a="sun"
b="haoy"
if [ $a = $b ]
then
echo "$a equal $b"
fi

if [ $a != $b ]
then
echo "$a not equal $b"
fi

if [ -n "$a" ] # 字符串长度不为0
then
echo "$a length is not zero"
fi

if [ -z $a ] # 字符串长度为0
then
echo "$a length is zero"
fi

  • 执行效果
    在这里插入图片描述

11. 数组操作

#!/bin/bash
# define array
arr=("aa" "bb" "hello world")
echo "all element in arr :${arr[@]}"
arr[1]="sun"
echo "index 1 element is ${arr[1]}"
echo "$ arr length is ${#arr[@]}"
  • 执行效果
    在这里插入图片描述

12. 分支操作

#!/bin/bash
age=18
if [ $age  -le 10 ]
then 
echo "shaonian"
elif [ $age -le 20 ]
then
echo "qingnian"
else 
echo "come on"
fi

status=6
case $status in 
0) echo "osun";;
1) echo "1sun";;
6) echo "6sun";;
esac
  • 执行效果
    在这里插入图片描述

13. 循环操作

#!/bin/bash

# for ... in ... do... done; while ... do... done; util ... do... done; break

arr=(aa bb cc)
for item in ${arr[@]}
do
echo "$item"
done
  • 执行效果
    在这里插入图片描述

14. 函数定义

#!/bin/bash
function myfun()
{
echo "this is a function about shell"
}
myfun

function add()
{
local ret=$[$1+$2]
return $ret
}
add 6 9
echo $? # output return value

$?属于内置特殊变量,可以获得当前函数的返回值

  • 执行效果
    在这里插入图片描述

15. 猜数游戏

#!/bin/bash
answer=$(date +%s%N | cut -c16-17)  # 生成一个随机数
count=0
while :
do
count=$[count+1]
echo -n "input number in 0-99 : "
# get user input 
read aNum  # 输入值
if [ $aNum -eq $answer ]
then
echo "you guess it by $count number! "
break;
elif [ $aNum -lt $answer ]
then 
echo "more bigger"
else
echo "more less"
fi
done
  • 执行效果
    在这里插入图片描述

16. ping网络状态

#!/bin/bash
test_ping()
{
local ip=$1
# 判断是否输入ip
if [ -z "$ip" ]
then 
echo "input ip please"
return
fi

ping -c1 $ip &>/dev/null
[ $? -eq 0 ] && "$ip on" || echo "$ip off"  # 类似C语言中的双目运算符
}

for ((i=1; i<19; i++))
do
test_ping "192.168.122.$i" &  # & stands for concurrent execution
done

wait
echo "finish!"
  • 执行效果
    在这里插入图片描述
    参考学习链接:https://www.bilibili.com/video/BV1ZZ4y1M7ua?p=4

猜你喜欢

转载自blog.csdn.net/qq_34720818/article/details/114937869