Exercise 1:Print a line with only one number
Requirements: title, a text document with only a number of lines to print out
Reference Answer:
#!/bin/bash# date:2018 March 16 f=/etc/groupline= ' wc-l $f |awk ' {print $} ' for L in ' seq 1 $line ' do n= ' Sed-n "$l" p $f |gre P-o ' [0-9] ' |wc-l ' if [$n-eq 1] then sed-n "$l" p $f fidone
Exercise 2: drawing a lottery script
Requirements:
1, write a script execution, enter the name, generate a random number 01-99 between the number.
2, if the same name repeated input, the captured number is the result of the first crawl,
3, the previous number has been captured, the next time can not appear the same number.
4, the first input name, the screen output information, and the name and number recorded in the file, the program can not quit
Continue to wait for other students to enter.
Reference Answer:
#!/bin/bash# date:2018 March 16 while :d o read -p "Please input your name: " name if [ -f /tmp/draw.log ];then a= ' cat /tmp/draw.log | awk -f ': ' ' { print $1} ' | grep ' $name ' if [ ' $a != ' $name ' ];then number= ' echo $RANDOM |awk -F "" ' {print $2 $3} ' while : do c= ' Cat /tmp/draw.log |awk -F ":" ' {print $2} ' |grep "$number" ' if [ "$number" == "$c" ];then echo "Number exist." number= ' Echo $RANDOM | awk -F " ' {print $2 $3} ' else break fi done echo "$name: $number" |tee -a /tmp/draw.log else number= ' cat /tmp/draw.log | grep "$name" | awk -F ":" ' {print $2} ' echo $number echo "Repeat name." fielse number= ' echo $RANDOM |awk ' {print $2  $3} " echo " $name: $number "|tee -a /tmp/draw.logfidone
Exercise 3: guessing numbers
Requirements: Write a guess the number of the script, when the user entered the number and the preset number (randomly generated a number less than 100), the direct exit, otherwise let the user input, and prompted the user's number is larger or smaller than the preset number.
Reference Answer:
#!/bin/bash# date:2018 March 16 n= ' echo $RANDOM |awk -F "" ' {print $2 $3} ' ' while :d o read -p ' please guess in [0-99]: " m expr $m + 1 >/dev/null 2>&1 if [ $? -ne "0" ];then echo -e "\033[41;37m please input number! \033[0m" continue fi if [ $n -eq $m ];then echo -e "\033[34m bingo! \033[0m" exit elif [ $n -lt $m ];then echo "bigger!" continue else echo "smaller!" continue fidone
Exercise 4: downloading files
Requirement: Create a function that can accept two parameters:
1. The first parameter is a URL, you can download the file, the second parameter is the directory, that is, the location of the download after the save;
2. If the user-given directory does not exist, create the directory
3. If the given directory exists, download the file; Download command after the execution of the test file download success or not; If successful, then > return 0 to the calling script, otherwise, return 2 to the calling script;
Reference Answer:
#!/bin/bash# date:2018 March 16 if [$#-ne 2];then echo "Usage $ URL dirname" Exit 1fi[!-D-$] && mkdir-p $2CD $2wget $1if [$?-eq 0];then echo "Download ok!" Exit 0else echo "FAIL!!!" Exit 2fi
Exercise 5: Listing subdirectories
Requirements: The script can take parameters or without, parameters can have more than one, each parameter must be a directory, the script checks the number of parameters, if equal to 0, the current directory itself is listed, otherwise, display each parameter contains subdirectories
Reference Answer:
#!/bin/bash# date:2018 March 16 if [$# = = 0];then ls-ld ' pwd ' else for I in ' seq 1 $# ' do a= $i Echo ' LS ${!a} "ls-l ${!a} |grep ' ^d ' Donefi
Exercise 6: Obtaining a network card IP
Requirements: Prompt the user to enter the name of the network card, then we use the script to output the IP card. Seemingly simple, but need to consider a number of aspects, such as we entered the name of the network card does not match the specification, how to deal with. The name conforms to the specification, but there is no way to deal with this NIC
Reference Answer:
#!/bin/bash# date:2018 March 16 while :d o read -p "Please input a network name: " e e1= ' echo " $e " | sed " S/[0-9]//g ' e2= ' echo "$e" | sed ' s/[a-za-z]//g ' if [ -z $e ];then echo "You input nothing " continue elif [ -z $e 1 ];then echo "You input number? but network name start with ens+number. " continue elif [ -z $e 2 ];then echo "You input letter ? but network named ens+number " continue else break fidoneip () { ifconfig| grep -wa1 "$:" |tail -1|awk ' {print $2} '}myip= ' ip $e ' if [ -z $ myip ];then echo "sorry, the network $e is not exist. " else echo "your network ip is: $myip" fi
Shell exercises (14)