shell交互式传参
# 交互式传参
read
# 相关示范
# sys.sh
#!/bin/bash
echo "Which Operating System are you using?"
echo "Windows, Android, Chrome, Linux, Others?"
read -p "Type your OS Name:" OS
case $OS in
Windows|windows|window|win)
echo "That's common. You should try something new."
echo
;;
Android|android)
echo "This is my favorite. It has lots of applications."
echo
;;
Chrome|chrome)
echo "Cool!!! It's for pro users. Amazing Choice."
echo
;;
Linux|linux)
echo "You might be serious about security!!"
echo
;;
*)
echo "Sounds interesting. I will try that."
echo
;;
esac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
which Operating System are you using?
Windows, Android, Chrome, Linux, Others?
Type your OS Name:linux
You might be serious about security!!
1
2
3
4
2
3
4
# ip-check.sh
ip检查
#!/bin/bash
read -p "please input a number:" -s IP(-p 后跟提示内容 -s 表示不显示输入的内容)
echo " "
ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
1
2
3
4
2
3
4
# ping.sh
根据退出判断该ip网是否通;sh ping.sh 192.168.11
#!/bin/bash
[ -z $1 ]&&{
echo "please input a IP"
exit 1
}
ping -c1 -w1 $1 &> /dev/null
[ $? = 0 ]&& echo $1 is up || echo $1 is down
1
2
3
4
5
6
7
2
3
4
5
6
7
# 用户文件和密码文件建立用户
#!/bin/bash
read -p "please input a userfile:" USER
[ ! -e $USER ] && {
echo "$USER is not exist"
exit 1
}
read -p "please input a passwdfile:" PASS
[ ! -e $PASS ] && {
echo "$PASS is not exist"
exit 1
}
MAX_LINE=`wc -l $USER | cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $USER`
PASSWD=`sed -n "${LINE_NUM}p" $PASS`
useradd $USERNAME
echo $PASSWD | passwd --stdin $USERNAME
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 使用函数作用
1.简化脚本内容,使脚本可读性更高。2.可以重复多次调用
#!/bin/bash
ACTION_ADD(){
[ "$1" = add ]&&{
read -p "please input a username:" USERNAME
read -p "please input a password:" -s PASSWORD
echo ""
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
}
}
ACTION_DEL(){
[ "$1" = del ]&&{
read -p "please input a username:" USERNAME
userdel -r $USERNAME
}
}
USER_CTL(){
read -p "please input action:" ACTION
[ "$ACTION" = exit ]&&{
echo bye~~
exit 0
}
ACTION_ADD $ACTION
ACTION_DEL $ACTION
USER_CTL
}
USER_CTL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
上次更新: 2022/04/15, 05:41:30