文章目录
一、脚本的创建
touch text.sh #创建脚本
chmod u+x text.sh #赋予脚本可执行的权限
后用用vim打开
vim text.sh
第一个程序
#! /bin/bash
echo "hello"
二、文件的输入输出
2.1 输出 echo
字符串使用时一定加上 $
#!/bin/bash
echo #单独的echo相当于换行
echo -e warning: \a #使用 -e命令时某些特殊符号不会显示原来的
#书上介绍 本人尝试没有成功
echo hello world
echo "hello world"
str="hello world"
echo $str
2.2 输入 read
#!/bin/bash
echo please input num
read num #可以输入变量
echo 变量为$num
read -t4 num2 #请在四秒内输入,-t4 可以避免无休止的等待
echo 变量为$num2
read -n10 num3 #限定字符数为4
echo 变量为$num3
read -s num4 #可以不显示输入的内容,以用来保密
echo 变量为$num4
三、特殊变量的使用
3.1 位置参数
符号 | 用途 |
---|---|
$0 | 为脚本名 |
$1 ~ &9 | 执行脚本时输入的第1~9个参数 |
$# | 参数的个数 |
$? | 参数返回值 |
# 列子
#!/bin/bash
#执行脚本时输入
echo 脚本名$0
echo 第1个参数$1
echo 输入参数个数$#
[ 3 -le 5 ] #判断是否1<2
echo "$?" #打印结果
输入运行
结果
常用案列
#! /bin/bash
[ "$1" == "a" ] && echo "a"
[ "$1" == "b" ] && echo "b"
运行结果
3.2 环境变量
$SHELL # 系统默认shell类型
$TERM # 终端类型
$USER # 用户名
$LS_COLORS # 颜色信息
$LOGNAME # 当前用户的登录名
$HOME # 当前用户的主目录
$PWD # 当前路径
$PATH # 环境变量路径
3.4引号的使用
单引号和双引号区别
单引号对特殊符号『$ \』作普通符号处理
单引号对特殊符号『$ ` \』还做特殊符号处理
代码
echo hello world
echo 'hello world'
echo "hello world"
echo
echo $HOME
echo '$HOME'
echo "$HOME"
结果
3.4倒引号
到引号的 作用是: 引号中的 命令执行结果 代替 命令本身
echo `who`
四、常用的测试命令
使用方式
- 使用test “条件表达式” 以此格式
test 3 -le 5
- 使用 [ expression ] 格式
[ 3 < 5 ]
echo "$?" #打印结果
如果正确返回0 错误返会1
4.1测试文件类型
符号 | 用途 |
---|---|
test -e text.txt | 文件是否存在 |
test -f text.txt | 文件是否存在并且是否是文件 |
test -d text.txt | 文件是否存在并且是否是目录文件 |
-r | 判断是否具有可读权限 |
-w | 判断是否具有可写权限 |
test text1 -nt text2 | text1是否比test2新 |
test text1 -ot text2 | text1是否比test2旧 |
test text1 -ef text2 | text1 test2是否相同 |
4.2测试字符串类型
符号 | 用途 |
---|---|
-n string | 是否为不空 |
-z string | 是否为空 |
string1 = string2 | 是否相同 |
string1 != string2 | 是否不同 |
#! /bin/bash
string1="aaa"
string2="aaa"
string3="bbb"
[ $string1=$string2]
echo "$?"
[ $string3=$string2]
echo "$?"
运行结果
0
1
4.3数值判断
符号 | 用途 |
---|---|
1 -eq 2 | 1和2是否相等 |
1 -ne 2 | 1和2是否不想等 |
1 -le 2 | 是否1<2 |
1 -lt 2 | 是否1<=2 |
1 -ge 2 | 是否1>2 |
1 -gt 2 | 是否1>=2 |
4.4复合测试
符号 | 用途 |
---|---|
! expression | 表达式结果取反 |
expression1 -a expression2 | 表达式1和表达式2同时为真 结果为真 |
expression1 -a expression2 | 表达式1和表达式2有一个为真 结果为真 |
五、结构化命令
5.1if结构
# if-then 结构
if expression
then
command
fi
# if-then-else 结构
if expression
then
command
else
command
fi
# if-then-elif-else结构
if expression
then
command
elif
command
else
command
fi
5.2 case结构
#基本格式
case 变量 in
parttern1) commands1;;
parttern2) commands2;;
*) commands);
esac
#举例
read num
case $num in
a) echo a;;
b) echo b;;
c) echo c;;
*) echo other;
esac
5.3 select结构
#基本格式
select 变量 in 变量列表
do
command
done
六、shell循环结构
6.1 for循环
#基本格式
for 变量 in 变量列表
do
command
done
#C格式的for循环
for(( i=1 ;i<=10;i++))
do
echo $i
done
#列子
for str in a b c d
do
echo $str
done
# 结果 a b c d
日常操作
for flie in $(ls /home)
do
echo $flie
done
# 结果为你的用户名
6.2 while循环
#基本格式
while text expression
do
command
done
#列子
a=10
while [ $a -ge 0 ]
do
echo $a
let a--
done
# 结果 10 9 8 7 6 5 4 3 2 1
6.3 until命令
与while基本一致
#基本格式
until text expression
do
command
done
6.4 break中断
#格式
break n
n > 1 时 可以选择跳出n层循环
n 为1 时 可省略 代表跳出本层循环
#列子
for (( i=1 ; i < 10 ; i++ ))
do
echo $i
if [ $i -eq 5 ]
then
break;
fi
done
#结果1 2 3 4 5
#列2
for (( i=1 ; i < 10 ; i++ ))
do
for (( j=1 ; j < 10 ; j++ ))
do
echo $i
if [ $i -eq 3 ]
then
break 2;
fi
done
done
#结果 1111111112222222223
6.5 continue命令
#列子
for (( i=1 ; i < 10 ; i++ ))
do
if [ $i -eq 5 ]
then
continue;
fi
echo $i
done
#结果 1 2 3 4 6 7 8 9