1. Determine the type of all files in the/var/directory
for files in /var/* ;do if [[ -f $files ]];then echo "$files exists and is a regular file." elif [[ -h $files ]] ;then echo "$files is a symbolic link." elif [[ -d $files ]] ;then echo "$files is a directory." else echo "$files is others." fidone
2, add 10 user user1-user10, password is 8 bit random characters
for user in user{1..10} ;do id $user &> /dev/null if [[ $? -ne 0 ]] ;then useradd $user#openssl rand -base64 6 ; uuidgen |cut -c8 pw=`openssl rand -base64 6` echo $pw|passwd --stdin $user &> /dev/null echo "user:$user, password : $pw" >> /root/pw_file else echo "Error:$user is exsit" exit fi if [ $user == "user10" ] ;then echo "user is created,password is in /root/pw_File" fidone
#删除10个用户
for i in {1..10} ;do userdel -r user$i ;done
3. 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
for files in /etc/rc.d/rc3.d/[KS]* ;do if [ `basename $files|cut -c1` == "K" ] ;then echo "$files stop" else echo "$files start" fidone
4, write the script, prompt to enter the value of positive integer n, calculate the sum of 1+2+...+n
read -p "please input a positive integer: " INTif [[ $INT =~ ^[0-9]+$ ]] && [[ `expr $INT` != 0 ]] ;then sum=0 for i in `seq $INT`;do let sum+=i done echo "sum=$sum"else echo "Error:your input is not positive integer"fi
5. Calculates the sum of all integers within 100 that can be divisible by 3
sum=0for i in `seq 3 3 100` ;do let sum+=idoneecho "sum=$sum"
for((i=3,sum=0; i<=100; i+=3,sum+=i));do echo -n doneecho "sum:$sum"
6, 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
read -p "please input IP:" IPIPC=`echo $IP |awk -F. ‘$1=255&&$2<=255&&$3<=255&&$4<=255{print "0"}‘`IPCHECK=`echo $IP |grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\."`if echo $IP |grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ;then echo "start>>>" >>/root/ipup.txt if [ $IPC -eq 0 ] ;then for IP1 in {0..254} ;do for IP2 in `seq 254`;do { ping -c1 -w1 $IPCHECK$IP1.$IP2 &> /dev/null [ $? -eq 0 ] && echo "The host $IPCHECK$IP1.$IP2 run up" >> /root/ipup.txt }& done wait done else echo "your IP $IP is not available" fielse echo "your IP $IP format is wrong"fi
7. Print 99 multiplication table
for i in {1..9};do for j in `seq $i`;do echo -en "$i*$j=$(($i*$j))\t" done echodone
8, 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
for i in {1..10};do NUM=`openssl rand -base64 6|tr -dc ‘[:alpha:]‘ |head -c10` touch /data/${i}${NUM}.htmldoneunset NUM
9. Print Isosceles triangle
read -p "please input the triangle number: " NUMfor n in `seq $NUM` ;do{ for a in `seq $[$NUM-$n]` ;do echo -n " " done for b in `seq $[2*$n-1]` ;do color=$[RANDOM%7+31] echo -en "\033[1;${color}m*\033[0m" done echo}&done
Shell Script Forloops