1, write the script/root/bin/createuser.sh, to achieve the following functions: Use a user name as a parameter, if the specified parameter of the user exists, it will show its existence, otherwise added; Displays the ID number of the added user and other information
#!/bin/bash
Read-p "Please input your username:" N
Useradd $n &>/dev/null
If ["$?" = = "0"]
Then
echo "User info:
cat /etc/passwd | grep -E "\<^$n\>"
"
Else
echo "User already exists"
Fi
2, write script/root/bin/yesorno.sh, prompt the user to enter Yes or no, and determine whether the user entered Yes or no, or other information
#!/bin/bash
Read-p "Please input yes or no:" N
Case $n in
[Yy] [Ee] [ss]| [Yy])
echo "You are input is yes"
;;
[Nn] [oo]| [Nn])
echo "You are input is no"
;;
*)
echo "You input are other info"
;;
Esac
3, write script/root/bin/filetype.sh, judge user input file path, display its file type (normal, directory, link, other file type)
#!/bin/bashread -p "please input file path: " nif [ -f "$n" ] then echo "$n is 普通文件"; elif [ -b "$n" ] then echo "$n is 块设备文件"; elif [ -c "$n" ] then echo "$n is 字符设备文件"; elif [ -h "$n" ] then echo "$n is 符号链接文件" elif [ -d "$n" ] then echo "$n is 目录文件"; elif [ -p "$n" ] then echo "$n is 管道文件"; elif [ -s "$n" ] then echo "$n is 套接字文件"; else echo "$n unknown"fi
4, write the script/root/bin/checkint.sh, determine whether the user input parameter is a positive integer
#!/bin/bashread -p "please input your charactor: " nm=`echo $n | sed -n -r ‘s/[0-9]//gp‘`if [ ! "n$m" == "n" ] then echo "please restart input" elif [ "$n" == "0" ] then echo "your input is 0"else echo "your input is 正整数"fi
5. Determine the type of all files in the/var/directory
#!/bin/bash
For n in/var/*
Do
If [-F "$n"]
Then
echo "$n is ordinary file";
elif [-B "$n"]
Then
echo "$n is block device file";
elif [-C "$n"]
Then
echo "$n is character device file";
elif [-H "$n"]
Then
echo "$n is symbolic link file"
elif [-D "$n"]
Then
echo "$n is directory file";
elif [-P "$n"]
Then
echo "$n is pipeline file";
elif [-S "$n"]
Then
echo "$n is socket file";
Else
echo "$n Unknown"
Fi
Done
6, add 10 user user1-user10, password for the specified character
#!/bin/bash for n in user{1..10} do useradd $n if [ "$?" = "0" ] then echo "12345678" | passwd --stdin $n else echo "用户存在" fi
7. The/ETC/RC.D/RC3.D directory has several files starting with K and beginning with S, respectively reading each file, the output starting with K is the file plus stop, the output starting with S is the filename plus start, such as K34filename stop S66filename Start
#!/bin/bashfor?i?in?/etc/rc.d/rc3.d/[SK]*?;doif?[?$(basename?$i?|?cut?-c1)?==?"K"?]?;then????echo?"`basename?$i`?stop"else????echo?"`basename?$i`?start"fidone
8, write the script, prompt to enter the value of positive integer n, calculate the sum of 1+2+...+n
#!/bin/bash
Read-p "Please input num:" N
For ((i=1;i<= "$n"; i++))
Do
sum=$[$sum + $i]
Done
Echo $sum
9. Calculates the sum of all integers within 100 that can be divisible by 3
#!/bin/bash
N=0
For i in {1..100};
Do
If [$[$i%3]-eq 0]
Then
n=$[$n + $i]
Fi
Done
echo "Sum= $n"
10, write a script, prompted to enter the network address, such as 192.168.0.0, determine the input network segment of the host online status
#!/bin/bash
f=mktemp /tmp/ping.XXXXXXX
Read-p "Please input want to test:" IP
n=echo $ip | cut -d‘.‘ -f1-3
For i in {1..255}
Do
{m= $n. $i
PING-W1-C1 $m &>/dev/null && echo "$m is up!" && echo $m >> $f
}&
Done
11. Print 99 Multiplication table
#!/bin/bash
For i in {1..9}
Do
For j in $ (seq 1 $i);d o
Echo-ne "${j}x${i}=$[i*j]\t"
Done
Echo
Done
12, create 10 HTML files in the/testdir directory, the file name format is the number n (from 1 to 10) plus a random 8 letters, such as: 1abcdefgh.html
#!/bin/bash
For i in {1..10}
Do
n=openssl rand -base64 64 | tr -dc ‘[a-zA-Z]‘ | head -c8
touch/testdir/$i $n.html
Done
13. Print Isosceles triangle
#!/bin/bash
Until read-p ' place input number ' line;d o
if [["$line" =~ [0-9]+]]; then
Continue
Fi
Done
For i in seq 1 $line
;d o
Space=$[line-i]
Star=$[2I-1]
for j in seq 1 $space
;
Echo-e "\c"
Done
for k in seq 1 $star
;
CLOCR=$[RANDOM%7+31]
Echo-e "\e[1;${clocr}m\e[0m\c"
Done
Echo
Done
14, write a script, for 100 of all positive odd sum
#!/bin/bash
N=0
For i in {1..100};
Do
If [$[$i%2]-eq 1]
Then
n=$[$n + $i]
Fi
Done
echo "Sum= $n"
15, write a script, prompted to enter the network address, such as 192.168.0.0, determine the input network segment host online status, and statistics online and offline host how much
#!/bin/bash
f=mktemp /tmp/ping.XXXXXXX
Read-p "Please input want to test:" IP
n=echo $ip | cut -d‘.‘ -f1-3
For i in {1..255}
Do
{m= $n. $i
PING-W1-C1 $m &>/dev/null && {echo "$m is up!"; Echo $m >> $f;}
}&
Done
g=cat "$f" | wc -l
j=$[255-$g]
echo "Up num: $g; Down num: $j "
16, scripting, printing 99 multiplication table
#!/bin/bash
For i in {1..9}
Do
For j in $ (seq 1 $i);d o
Echo-ne "${j}x${i}=$[i*j]\t"
Done
Echo
Done
17, write a script, use the variable random to generate 10 random numbers, output this 10 number, and display the maximum and minimum values
#!/bin/bash
Let i=0,min=max= $RANDOM
echo "$min"
While [$i-lt 9];d o
ran= $RANDOM
echo "$ran"
If [$ran-ge $max];then
Let Max=ran
Fi
If [$ran-le $min];then
Let Min=ran
Fi
Let I+=1
Done
echo "Max is: $max, Min is: $min"
18, write a script to achieve the printing chess board
#!/bin/bash
Let X=y=1
H=8
While [$x-le $h];d o
While [$y-le $h];d o
If [$[(x+y)%2]-eq 0]; then
Echo-en "\033[47m \033[0m"
Else
Echo-en "\033[40m \033[0m"
Fi
Let Y+=1
Done
Echo
Let X+=1,y=1
Done
19, the following six strings: EFBAF275CD, 4be9c40b8b, 44B2395C46, F8c8873ce0, b902c16c8b, ad865d2f63 are randomly executed by random number variable random command: Echo $RANDOM | After md5sum|cut–c1-10, break the random values of these strings
#!/bin/bash
Ch= (efbaf275cd 4be9c40b8b 44b2395c46 f8c8873ce0 b902c16c8b ad865d2f63)
For num in seq 0 65535
;d o
chnum=echo $num | md5sum | cut -c 1-10
For n in ${ch[*]}; Do
If ["$chnum" = = "$n"];then
echo "$n-$num"
Fi
Done
Done
20, every 3 seconds to the system to obtain the information of the user who has logged on, if the user hacker log on, the logon time and the host record in the log/var/log/login.log, and exit the script
#!/bin/bashusername=cent{while?true?;do????if?who?|?egrep?"^\b$username\b"?&>?/dev/null?;then????????who?|?egrep?"^\b$username\b"?>>?/var/log/login.log????????echo?"$username?已经登录"????????echo?"fuck?,go?out?my?system"?|?write?$username????else????????echo?"$username?已经下线"????fi????sleep?3done}
21, randomly generated within 10 of the number, to achieve the word guessing game, the hint is relatively large or small, equal to exit
#!bin/bashlim=10let?key=$RANDOM%limread?-p??"please?input?a?number?less?than?$lim:?"?numuntil?false;do????if?[?$num?-le?$lim?&>?/dev/null??]?&&?[?$num?-ge?0??&>?/dev/null?]?;then????????if?[?$num?-lt?$key?];then????????????read?-p?"is?small,please?input?again:?"?num????????elif?[?$num?-gt?$key?];then????????????read?-p?"is?big,please?input?again:?"?num????????else????????????echo?"you?are?right,you?are?very?clever"????????????break????????fi????else????????read?-p??"error,please?input?a?number?less?than?ten:?"?num????fidone
22, with the file name as a parameter, the total number of all parameters file statistics
#!/bin/bash
Let Z=0
While read-p "Please input file path:" N
Do
m=cat "$n" | wc -l
z=$[$z + $m]
echo "Sum line: $z"
if (("$z" > "5000")
Then
echo "Cache would be"
Break
Fi
Done
23, with more than two numbers as parameters, showing the maximum and minimum values
#!/bin/bash while read -p "please input two number: " n m do if (("$n" > "$m")) then max=$[$n] min=$[$m] echo "max=$max; min=$min" elif (("$n" < "$m")) then max=$[$m] min=$[$n] echo "max=$max; min=$min" else echo "two num equal" fidone
Shell Scripting Exercises