1小时学会Shell Scripting

1小时学会Shell Scripting
最近学了下shell.这个教程很紧凑全面.这里上传代码

#!/bin/bash
# 空格问题
# 变量赋值两边无空格
# 条件测试两边均空格
# 条件测试字符串比较 比较符号两端空格 [ $gender = female ]
# expr [[]]
# Comment 中间看不懂的,请先看单双引号的意思
echo "Hello World"

# define variable
myName="Qinning"
declare -r NUM1=5
num2=4

echo ---------operator{}---------
# 1.修饰变量,给变量赋默认值
# 2.${var%pattern}, ${var%%pattern}, ${var#pattern}, ${var##pattern},
# ${var//dog/cat}
# 3.枚举 touch {1,2,3}_file; touch {1..5}_file

echo ---------operator'(())'用于算数运算比较---------
# operate (())
num3=$((NUM1%num2)) # 双括号前面带$ 则获得表达式值,赋值给左边变量
echo "5 % 4 = $num3"
a=1
((a++))
a=$((a+1,a--,(a+1)**10)) # (())中可以逗号. 依次计算,取最后一个值
echo $a
rand=5
let rand+=4
echo hello, $rand
echo "rand++ = $((rand++))"
echo "++rand = $((++rand))"
echo "rand-- = $((rand--))"
echo "--rand = $((--rand))"

echo ---------logic---------
for((i=0;i<=5;i++))
do
    echo $((i**2<10?i**2:10)) # 三目
done
((i<2)) || ((i>3)) && echo '成功执行||和&&' # &&正确则执行,||错误则执行

if ((((i%3))==0))
then
    echo "$i是3的倍数"
fi

num1=0
while [ $num1 -le '20' ]
do
	if (((($num1%3))==0))
	then
		echo $num1
	fi
	((num1++))
done

until [ $num1 -le 5 ]
do
	num1=$(($num1-4))
	echo $num1
done

echo ---------'""',"''",'``', '$(())'---------
# 双引号,解析变量,解析`命令Or变量`,$(命令Or变量)
echo "$i>3"
echo '$i>3' # 单引号,纯字符串
echo $i>3 # 将i值写入了3文件
echo $((i>3)) # 1 $(())
echo `expr $i>3` # expr
echo `echo 反引号`
echo $(echo '$()') #$()新开一个shell执行,同时,命令替换只替换标准输出,去除错误输出

echo `seq 3`;echo "`seq 3`"

echo --------shell special variable --------
echo '$#'"=$#" # Shell 参数个数
echo '$$'"=$$" # Shell PID
echo '$!'"=$!" # Shell most recent background command
echo '$?'"=$?" # Shell 返回值
for p in $*
do
    echo $p
done

echo --------shell function--------
getDate(){
    date
    return
}
getDate

getSum(){
    local num1=$1
    local Num2=$2
    local sum=$((num1+Num2))
    echo $sum
}
sum=$(getSum NUM1 num2)
echo "$NUM1+$num2=$sum"

echo ---------read以及operator[[]] 用于字符串比较---------
# [[]]是[]的增强版本
# read -p "What's your age?" age
if [ "${age:=15}" -ge '16' ] # []内变量要加上"" [[]]则不用
then
	echo "You can fuck."
elif [ "$age" -eq '15' ]
then
	echo "You can fuck next year."
else
	echo "You can't fuck."
fi

# [ -d samp_dir ] || mkdir samp_dir
str1=""
str2="lugubrious"
str3="jubilant"

if [ "$str1" ]
then
	echo "$str1 is not null"
fi

if [ -z "$str1" ]
then
	echo "str1 has no value"
fi

if [ "str2" == "str3" ]
then
	echo "$str2 equals $str3"
elif [ "str2" != "str3" ]
then
	echo "$str2 is not equal to $str3"
fi

if [ "str2" > "str3" ]
then
	echo "$str2 is greater than $str3"
elif [ "str2" <= "str3" ]
then
	echo "$str2 is less than $str3"
fi

file1="./test_file1"
file2="./test_file2"

touch $file1 && chmod u+x $file1
mkdir $file2

if [ -e "$file1" ]
then
	echo "$file1 exists"
fi

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

if [ -r "$file1" ]
then
	echo "$file1 is readable"
fi

if [ -w "$file1" ]
then
	echo "$file1 is witable"
fi

if [ -x "$file1" ]
then
	echo "$file1 is executable"
fi

if [ -d "$file2" ]
then
	echo "$file2 is a directory"
fi

echo --------regular expression--------
# read -p "Validate Date : " date
date1=12121212
pat="^[0-9]{8}$"

if [[ $date1 =~ $pat ]]
then
	echo "$date1 is valid"
else
	echo "$date1 is not valid"
fi

OIFS="$IFS"
IFS=","
# read -p "Enter 2 Numbers to add separated by a comma : " num1 num2
num1=1
num1=${num1//[[:blank:]]/}
num2=${num2//[[:blank:]]/}
sum=$((num1+num2))
echo "$num1+$num2=$sum"
IFS="$OIFS"

# read -sp "Enter the secret code" secret
secret=password
if [[ "$secret" == "password" ]]
then
	echo Enter
else
	echo Wrong Password
fi

echo --------case option很奇怪,记得后缀,记得括号--------
case ${age:+2} in
	[0-4])
		echo "Too young for school"
		;;
		5)
echo "Go to Kindergarten"
;;
[6-9]|1[0-8])
grade=$((age-5))
echo "Go to grade $grade"
;;
*)
echo "You are too old for school"
esac

can_vote=0
((age>=18?(can_vote=1):(can_vote=0)))

echo --------string--------
rand_str="A random string"
echo "String Length: ${#rand_str}"
echo "${rand_str:2}"
echo "${rand_str:2:8}"
echo "${rand_str#*A}"

echo --------shell call python--------
num7=1.2
num8=3.4
num9=$(python -c "print($num7+$num8)")
echo $num9

cat<<END
This text 
prints on
many lines
END
echo $num7 $num8 $num9>test_file1

echo --------shell reading from files/arguments--------
while read n1 n2 n3;
do printf "n1:$n1\nn2:$n2\nn3:$n3\n"
done < test_file1

echo --------shell array--------
fav_nums=(3.14 2.718 .57721 4.6692)
echo "Pi: $fav_nums[0]"
fav_nums[4]=1.618
fav_nums+=(1 7)
for i in ${fav_nums[*]}; do
	echo $i
done

echo "Array Length: ${#fav_nums[@]}"
echo "Index 2 Length: ${#fav_nums[3]}"

sorted_nums=($(for i in "${fav_nums[@]}"
do
	echo $i
done | sort
	))

unset 'sorted_nums[1]'
for i in ${sorted_nums[*]}; do
	echo $i
done
unset sorted_nums

结果

Hello World
---------operator{}---------
---------operator(())用于算数运算比较---------
5 % 4 = 1
1024
hello, 9
rand++ = 9
++rand = 11
rand-- = 11
--rand = 9
---------logic---------
0
1
4
9
10
10
成功执行||和&&
6是3的倍数
0
3
6
9
12
15
18
17
13
9
5
---------"",'',``, $(())---------
6>3
$i>3
1

反引号
$()
1 2 3
1
2
3
--------shell special variable --------
$#=0
$$=2938
$!=
$?=0
--------shell function--------
2021年 02月 05日 星期五 03:40:08 CST
5+4=9
---------read以及operator[[]] 用于字符串比较---------
You can fuck next year.
str1 has no value
lugubrious is not equal to jubilant
lugubrious is greater than jubilant
./test_file1 exists
./test_file1 is a normal file
./test_file1 is readable
./test_file1 is witable
./test_file1 is executable
./test_file2 is a directory
--------regular expression--------
12121212 is valid
1+4=5
Enter
--------case option很奇怪,记得后缀,记得括号--------
Too young for school
--------string--------
String Length: 15
random string
random s
 random string
--------shell call python--------
4.6
This text 
prints on
many lines
--------shell reading from files/arguments--------
n1:1.2
n2:3.4
n3:4.6
--------shell array--------
Pi: 3.14[0]
3.14
2.718
.57721
4.6692
1.618
1
7
Array Length: 7
Index 2 Length: 6
1
2.718
3.14
4.6692
.57721
7
```Hello World
---------operator{}---------
---------operator(())用于算数运算比较---------
5 % 4 = 1
1024
hello, 9
rand++ = 9
++rand = 11
rand-- = 11
--rand = 9
---------logic---------
0
1
4
9
10
10
成功执行||和&&
6是3的倍数
0
3
6
9
12
15
18
17
13
9
5
---------"",'',``, $(())---------
6>3
$i>3
1

反引号
$()
1 2 3
1
2
3
--------shell special variable --------
$#=0
$$=2938
$!=
$?=0
--------shell function--------
2021年 02月 05日 星期五 03:40:08 CST
5+4=9
---------read以及operator[[]] 用于字符串比较---------
You can fuck next year.
str1 has no value
lugubrious is not equal to jubilant
lugubrious is greater than jubilant
./test_file1 exists
./test_file1 is a normal file
./test_file1 is readable
./test_file1 is witable
./test_file1 is executable
./test_file2 is a directory
--------regular expression--------
12121212 is valid
1+4=5
Enter
--------case option很奇怪,记得后缀,记得括号--------
Too young for school
--------string--------
String Length: 15
random string
random s
 random string
--------shell call python--------
4.6
This text 
prints on
many lines
--------shell reading from files/arguments--------
n1:1.2
n2:3.4
n3:4.6
--------shell array--------
Pi: 3.14[0]
3.14
2.718
.57721
4.6692
1.618
1
7
Array Length: 7
Index 2 Length: 6
1
2.718
3.14
4.6692
.57721
7

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值