Advanced shell scripts (for, while, if, case)

Source: Internet
Author: User
Tags stdin egrep

1, write script/root/bin/createuser.sh, to achieve the following functions: Use a user name as a parameter, if the user specified parameter exists, it will show its existence, otherwise add, and generate a 8-bit random password and there is a file, the initial prompt to change the password, Displays information such as the ID number of the added user.

#!/bin/bash# ------------------------------------------#  filename: cid.sh # date: 2017-09-16# author: liuke# description:    Use a user name as a parameter, if the specified parameter of the user exists, it will show that it exists, otherwise add #  generate 8 random password and there is a file, the initial prompt to change the password, display the ID number of the added user and other information. # -------------------------------------------read -p  "Please input a username:"  unameid  $uname  &> /dev/nulle=$?if [  $e  -eq 0 ]then     echo  "$uname  is exist"     exit 2else     useradd  $uname     echo  ' cat  /dev/urandom  |  tr -d -c  ' [: alnum:][:p UNCT:] '  | head -c 10 '  > /app/mima     passwd  $uname     id  $unamefiunset  uanme e 

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#------------------------------------------# Filename:yesno.sh # revision:null# date:2017-09-10# Author: liuke# Description: Prompts the user to enter Yes or no, and to determine whether the user entered Yes or no, or other information #-------------------------------------------read-p "Yue ma Yes or no: "Wordword= ' echo $word |tr" A-Z "" A-Z "' Case $word Inyes|y) echo" Yue ";; No|n) echo "Buyue";; *) echo "error input" Esac

3, write script/root/bin/filetype.sh, judge user input file path, display its file type (normal, directory, link, other file type)

#!/bin/bash#------------------------------------------# Filename:filetype.sh # date:2017-09-11# author:liuke# Description: Determine the user input file path, display its file type #-------------------------------------------read-p "Please input a file:" Fileif [-F $f      Ile];then echo "filetype is common file" elif [-D $file];then echo "filetype is directory" elif [-H $file];then echo "filetype is soft link" else echo "filetype was other" Fiunset file

4, write the script/root/bin/checkint.sh, determine whether the user input parameter is a positive integer

#!/bin/bash#------------------------------------------# Filename:checkint.sh # date:2017-09-11# author:liuke# Description: Determine if the user input parameter is a positive integer #-------------------------------------------read-p "Please input a num:" numif [[$num =~ ^[ 1-9][0-9]*$]];then echo "The num is a positive integer" else echo "wrong input" fi

5. Determine the type of all files in the/var/directory

#!/bin/bash# ------------------------------------------# filename: file-var.sh # date : 2017-09-11# author: liuke# description:  determine the type of all files under the/var/directory # ------------------- -----------------------cd /var/;ls -1 | while read filenamedo     if [ -f  $filename  ];then         echo   "  $filename  filetype is common file"     elif [ -d   $filename  ];then         echo  $filename   Filetype is directory "    elif [ -h  $filename  ];then          echo  "$filename  filetype is soft link "    else         echo " $filename   Filetype is&nbSp;other "    fidone         unset  FileName

6, add 10 user user1-user10, password is 8 bit random characters

#!/bin/bash# ------------------------------------------#  filename: 10user.sh # date: 2017-09-11# author: liuke# description:  Add 10 user User1-user10, password 8-bit random character # -------------------------------------------for  (i=1;i<= 10;i +=1))      do        pw= ' Cat /dev/urandom  |tr -d -c  ' [: alnum:][:p UNCT:] ' |head -c 8 '          useradd  user$i && echo  $PW |passwd --stdin user$i  &> /dev/null        echo -e  "USER:USER$I;PASSWD: $PW "     done# for i in {1..10};d O userdel -r user $i;d one   Delete 10 users at a time 

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/bash#------------------------------------------# Filename:rc3.sh # date:2017-09-16# author:liuke# Description: Read/etc/rc.d/rc3.d/Each file separately, the output starting with K is the file plus stop, the output starting with S is the filename plus start#--------------------------------------- ----for filename in ' ls/etc/rc.d/rc3.d/';d ocase $filename ink*) echo "$filename stop";; s*) echo "$filename start" Esacdoneunset filename

8, write the script, prompt to enter the value of positive integer n, calculate the sum of 1+2+...+n

#!/bin/bash#------------------------------------------# filename:1-nhe.sh # date:2017-09-11# author:liuke# Description: Prompts for the value of positive integer n, calculates the sum of 1+2+...+n #-------------------------------------------Read-p "Please input a num:" NUMIF [[ $num =~ ^[1-9][0-9]*$]];then sum=0 for ((i=1;i<= $num; i+=1)) do sum=$ (($sum + $i)) d The sum of one echo "1+2+...+n: $sum" Fiunset num sum

9. Calculates the sum of all integers within 100 that can be divisible by 3

#!/bin/bash#------------------------------------------# Filename:zhengchu3.sh # date:2017-09-11# author:liuke# Description: Calculates the sum of all integers within 100 that can be divisible by 3-------------------------------------------sum=0for ((i=3;i<= 100;i+=3)) do s um=$ (($sum + $i)) done echo "100 The sum of all integers that can be divisible by 3: $sum" unset sum

10, write the script, prompt please enter network address, such as 192.168.0.0, determine the status of the host online in the input network segment.

#!/bin/bash# ------------------------------------------#  filename: scanip11-2.sh # revision: null# date: 2017-09-10# author:  liuke# description:  Prompt Please enter the network address, such as 192.168.0.0, determine the input network segment of the host online status. # -------------------------------------------> /app/ip.txtread -p  "Please input  a ip: " ipecho  $ip |egrep  " ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) "&> /dev/nullif [ $? -eq 0 ];then      id= ' echo  $ip |egrep -o  ". *\." '      ping -c 1 -w 1  $ip  &> /dev/null      echo  "$ip  is up" >>/app/ip.txt     echo   "$ip  is down" fi 

11. Print 99 multiplication table (for loop)

#!/bin/bash#------------------------------------------# filename:9*9.sh # revision:null# date:2017-09-10# Author:   liuke# Description: Print 99 multiplication table (for loop) #-------------------------------------------for i in {1..9};d o for j in ' seq $i ';d o Echo-en "${i}x${j}=$[$ix $j]\t" Done Echodone

12. Print 99 multiplication table (while loop)

#!/bin/bash#------------------------------------------# filename:9*9while.sh # date:2017-09-11# author:liuke# Description: Print 99 multiplication table (while) #-------------------------------------------while ((i<10,j<9)) do Echo-en "${i}x${ j}=$[$i * $j |bc]\t "done Echo

13, 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#------------------------------------------# filename:10user.sh # date:2017-09-11# author:liuke#     Description: Add 10 user user1-user10, password 8-bit random character #-------------------------------------------for ((i=1;i<= 10;i+=1)  Do pw= ' cat/dev/urandom |tr-d-C ' [: alnum:][:p UNCT:] ' |head-c 8 ' useradd user$i && echo $PW |passwd --stdin user$i &>/dev/null echo-e "user:user$i;passwd: $PW" done# for i in {1..10};d o userdel-r user$ I;done Delete 10 users at a time

14, write a script, for 100 of all positive odd sum

#!/bin/bash#------------------------------------------# Filename:jishuhe.sh # date:2017-09-11# author:liuke# Description: Write a script that asks for all positive and odd numbers within 100 and #-------------------------------------------Sum=0;i=1while ((i<100)) do sum=$[$ Sum + $i] Let i=$[$i +2]done echo "100 all positive odd sum: $sum"

15. write a script that generates 10 random numbers using the variable random, outputs the 10 numbers, and displays the maximum and minimum values.

#!/bin/bash# ------------------------------------------# filename: random10-2.sh #  date: 2017-09-16# author: liuke# description:  uses the variable random to generate 10 random numbers, outputting the 10 numbers, and displays the maximum and minimum values #              while#  -------------------------------------------i=1while [  $i  -lt 11 ];d o                 sum= ' Echo $[RANDOM] '                 echo -e   "$sum"   "\c"                  if [  $i  -le 1 ];then                         max= $sum   min= $sum                 elif [[  $sum  -gt  $max  ]];then                         max= $sum                  elif [[  $sum  -lt  $min  ]];then                          min= $sum                  fi                 let i+=1done                 echo                 echo  "the Number max is: $max; Min number is: $min " 

16. Write scripts, edit menus, the user enters a number in the menu list to display the corresponding price.

#!/bin/bash#------------------------------------------# Filename:caidan.sh # date:2017-09-10# author:liuke# Description: Edit menu, user input A number in the menu list, display the corresponding price. #-------------------------------------------echo "1=yangroutang2=mifan3=hulatang 4=jiaozi5=lamian6=huimian" read- P "Please input your choose num:" Numcase $num in1|4) echo "The Price is 20¥";; 2|5) echo "The Price is 12¥";; 3|6) echo "The Price is 10¥";; *) echo "Error Choose" Esac

17, write a script to achieve the printing chess board

#!/bin/bash# ------------------------------------------# filename: chess11.sh # date:  2017-09-11# Author: liuke# Description:  Board # ----------------------------------- --------for  ((i=1;i<=8;i++));d o    for  ((j=1;j<=8;j++));d o        total=$ (($i + $j))       tmp=$ (($total  % 2) )           if [  $tmp  -eq 0 ]                then                     echo  -e -n  "\033[43m  \033[0m"                 else                     echo -e -n  "\033[41m  \033[0m"            fi    done    echodone

This article is from the Linux file system blog, so be sure to keep this source http://13250262.blog.51cto.com/13240262/1966066

Advanced shell scripts (for, while, if, case)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.