09 shell编程基础
目录
1.第一个SHELL脚本
#!/bin/bash
echo "helloworld"
2.变量与局部变量
bash中变量都默认被存储为字符串
(1)变量与局部变量:
#!/bin/bash
a=19
b="hello world"
c=helloworld
echo ${a} ${b} ${c}
echo ${a}
echo ${b}
echo ${c}
echo -n ${a}
echo -n ${b}
echo -n ${c}
function print(){
var=12
local local_var=10
}
print
echo ${local_var}
echo ${var}
(2)特殊变量:
位置变量 | 说明 |
---|---|
$0 | 获取当前执行shell脚本的文件名,包括路径 |
$n | 获取当前执行脚本的第n个参数,n = 1…9,如果n大于9则需要将n使用大括号括起来 |
$* | 获取当前shell的所有参数,将所有命令行参数视为单个字符串,相当于"$1$2$3"; |
$# | 获取执行当前脚本的参数个数 |
$@ | 获取这个程序所有参数,并保留参数之间的任何空白,相当于"$1" “$2” “$3” (这是将参数传递给其他程序的最好方法) |
状态变量 | 说明 |
---|---|
$? | 判断上一指令是否成功执行,0为成功,非0为不成功 |
$$ | 获取当前进程的PID |
$! | 获取上一指令的PID |
#!/bin/bash
echo "位置变量:"
echo $0
echo $1 $2
echo $*
echo $#
echo $@
echo "状态变量"
echo $?
echo $$
echo $!
3.输入输出
(1)read:
grammar:read [-options] [variable...]
-a array:把输入赋值到数组array中,从索引号0开始
-d delimiter:用字符串delimiter中的第一个字符指示输入结束,而不是一个换行符
-e:使用Readline来处理输入,这使得与命令行相同的输入方式编辑输入
-n num:读取num个输入字符,而不是整行
-p prompt:为输入显示提示信息,使用字符串prompt
-r:Raw mode不把反斜杠字符解释为转义字符
-s:Silent mode,输入密码不显示
-t seconds:指令相应时间设置,如果超出设置时间自动执行其他命令
-u fd:使用文件描述符fd中的输入,而不是标准输入
#!/bin/bash
read -a arr
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
read -s -p "please input a passwd : " passwd
echo ${passwd}
(2)printf:
grammar:printf format-string [arguments...]
#!/bin/bash
printf "|%10s|%10s|%10s|\n" "kkber" "hello" "you"
printf "|%-10s|%-10s|%-10s|\n" "123" "456" "789"
printf "|%10s|%10s|%10s|\n" "qwe" "rty" "uio"
printf "|%10s|%10s|%10s|\n" "asdfg" "ghjkl" "fghjk"
printf "|%10s|%10s|%10s|\n" "time" "is" "limited"
(3)echo:
grammar:echo string
echo与printf函数的区别:echo语句结束后程序会自动添加一个换行符号,而printf语句不会自动添加
4.函数:
#!/bin/bash
function printf1 {
echo "helloworld"
return
}
printf2() {
echo "helloworld"
return
}
function printf3() {
echo "helloworld"
return
}
printf1 "helloworld"
printf2 "helloworld"
printf3 "helloworld"
5.流程控制
(1)流程控制if:
#! /bin/bash
if [[ condition ]]; then
#statements
fi
if [[ condition ]]; then
#statements
else
#statements
fi
if [[ condition ]]; then
#statements
elif [[ condition ]]; then
#statements
elif [[ condition ]]; then
#statements
else
#statements
fi
补充1:if判断条件的两种书写方式:
#! /bin/bash
read a
if test ${a} -eq 10; then
echo "a is 10"
else
echo "a is not 10"
fi
#! /bin/bash
read a
if [[ $[a] -eq 10 ]]; then
echo "a is 10"
else
echo "a is not 10"
fi
练习1:使用if的-n
选项实现字符串是否为空判断
#! /bin/bash
read str
if [[ -n ${str} ]]; then
echo "str is not null"
else
echo "str is null"
fi
练习2:使用-ge
、-lt
对数字判断
#! /bin/bash
read -p "please input a number : " number
if [[ ${number} -ge 100 ]]; then
echo "number is greater than or equal to 100"
else
echo "number is less than 100"
fi
#! /bin/bash
read -p "please input a number : " number
if [[ ${number} -lt 60 ]]; then
echo "sad"
elif [[ ${number} -lt 80 ]]; then
echo "good"
else
echo "great"
fi
练习3:判断数字的奇偶性
#!/bin/bash
read -p "please input a number : " number
if [[ $[${number}%2] -eq 0 ]]; then
echo "number is even"
else
echo "number is odd"
fi
(2)流程控制for:
#! /bin/bash
for i in words; do
#statements
done
for (( i = 0; i < 6; i++ )); do
#statements
done
补充1:for循环条件书写的四种书写方式
#!/bin/bash
for i in `seq 1 10`; do
echo "method1:seq" $i
done
for i in {1..10}; do
echo "mehtod2:1..10" $i
done
for i in {a..h}; do
echo "method3:a..h" $i
done
for(( i = 0; i < 10; ++i)); do
echo $i
done
练习1:for循环实现打印当前目录下所有文件名称
#!/bin/bash
for i in `ls`; do
echo $i
done
(3)流程控制while与until循环:
#!/bin/bash
while [[ condition ]]; do
#statements
done
#!/bin/bash
until [[ condition ]]; do
#statements
done
(4)流程控制case:
#!/bin/bash
case word in
pattern1 )
;;
pattern2 )
;;
pattern3 )
;;
esac
练习1:case的基本使用
#!/bin/bash
read -p "please input a number: " i
case $i in
1 )
echo "2020"
;;
2 )
echo "2021"
;;
3 )
echo "2022"
;;
esac
6.数组
数组主要有索引数组和关联数组,在bash解释器下索引数组的下标从0开始,在zsh解释器下索引数组的下标从1开始
(1)数组定义:
索引型数组
grammar:declare -a arr
name[subscript]=value
name=(value1 value2 …)
关联型数组
grammar:declare -A arr
(2)数组操作:
数组操作 | 命令 |
---|---|
输出数组内容 | ${arry[*]} 、${arry[@]} |
确定数组元素个数 | ${#arry[@]} |
找到数组下标 | ${!arry[@]} |
数组追加 | arry+=(a b c) |
数组排序 | sort |
删除数组与元素 | unset |