[知识点总结] shell 学习

shell 学习

shell的几点注意事项:

  1. 开头需标注脚本型 → #!/bin/bash
  2. 在第一次使用脚本的时候,需要激活权限chmod +x hello_world.sh

传参

  • $:变量声明符号
  • #:一般用于形容个数
标识符号作用
$#传递到脚本的参数个数传递到脚本的参数个数
$*将传入参数以字符串的形式展示
$$脚本运行的当前进程ID号
$!后台运行的最后一个进程号PID
$@与$*相同,但是是依次传入,不是以完整字符串的形式
$-显示Shell使用的当前选项
$?显示最后命令退出的状态,0表示没有错误
#!/bin/bash

echo "shell Pass parameter instance verification"
echo "The value of the first parameter is: $1"
echo "All parameters is: $*"
echo "the PID of this shell is: $$"
echo "the lastest PID is: $!"
echo "show the set of the shell: $-"
echo "show the lastest quit state: $?"
./chuan_can_test.sh 1 2 3 4 5 6

shell Pass parameter instance verification
The value of the first parameter is: 1
All parameters is: 1 2 3 4 5 6
the PID of this shell is: 1166
the lastest PID is: 
show the set of the shell: hB
show the lastest quit state: 0

常用指令

echo

echo 用于显示内容,一般后面跟双引号,或单引号

  • 双引号:内部内容可以通过$来进行更换,可以有变量,允许存在转义字符
  • 单引号:内部内容固定无法更换,直接形成一个字符串
echo ""
echo ''

unset

unset 用于删除变量

a="haha"
unset a

基本使用方法

中括号的使用

基本用途:1、算术比较 2、文件属性测试 3、字符串比较

大括号的使用

基本用途,限定变量的边界

for skill in a b c d;do
	echo "I can do ${skill}program"
done

shell 数组

array=(值1 值2 …… 值3)

数学运算符

`expr $a + $b`	# a + b
`expr $a - $b`	# a - b
`expr $a \* $b`	# a * b 
`expr $a / $b`	# a / b
a=$b	# 将b赋值给a
[ $a == $b ]	# 判断a和b是相等的吗
[ $a != $b ]	# 判断a和b是不等的吗

注:方括号中的运算符必须用空格隔开

关系运算符

运算符说明举例
-eq检测两个数值是否相等,相等返回true[ $a -eq $b ]
-ne检测两个数值是否不相等,不相等返回true[ $a -ne $b ]
-gt检测左边的数是否大于右边的数[ $a -gt $b ]
-lt检测左边的数是否大于右边的数[ $a -lt $b ]
-ge检测左边的数是否大于等于右边的数[ $a -ge $b ]
-le检测左边的数是否小于等于右边的数[ $a -le $b ]

流程控制

if

if condition
then
	command1
	command2
fi

if else

if condition
then
	command1
	command2
else
	command3
fi

if else if

if condition
then
	command1
	command2
elif condition
then
	command3
else
	command4
fi

for

for var in a b c d
do
	command1
done

while

while condition
do
	command1
done

shell 函数

function DoSomething()
{
	action
	return # 可有可无
}
DoSomething()

Linux指令

…待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值