Write a 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/bashread-p "Put your answer (yes or no):" Answercase $Answer iny| y| [YY] [EE] [SS]) Determine all possibilities for user input Yes echo "you agree";; n| n| [NN] [OO]) Determine all possibilities for user input no echo "you disagree";; *) echo "Please input the right answer" ESAC
2. Write a script/root/bin/filetype.sh, determine the user input file path, display its file type (normal, directory, link, other file type)
#!/bin/bashread-p "Input the path:" Pathif [! -e $Path];then//[] Medium-E To determine if there is an echo "No such file" Exitfi if [-F $Path];then echo "$Path is Common file" elif [-D $Path];then echo "$Path is Directory" elif [-L $Path];then echo "$Path is Symbol file" Else echo "Unko Wn "fi
3. Print a positive triangle with *
#!/bin/bash#version 1.0read-p "Please input the linenumber:" Linenumberfor Lineid in ' seq 1 $Linenumber ' does for A in ' seq 1 $[$Linenumber-$Lineid] '//blank first, then hit * do Echo-n "" Done for B in ' seq 1 $[2* $Lineid-1] ' Do echo-n ' * " Doneechodone
The following results are performed:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/83/42/wKiom1duMc6hEm99AABAZBys0ck473.jpg-wh_500x0-wm_3 -wmp_4-s_899528837.jpg "title=" 222.jpg "alt=" Wkiom1dumc6hem99aabazbys0ck473.jpg-wh_50 "/>
4. Write a script, prompting the user to enter a user name, first determine whether the user exists, if not exist, then create a user, and then set the password with the user name
#!/bin/bash# #read-P "input an username:" Usernamegrep "^ $Username \>"/etc/passwd &>/dev/nullif [$? -eq 0];then Echo-e "The \033[31m$username\033[0m is exist"//ECHO-E \033[31m***\033[0m for Will * * * Coloring Else Useradd $Usernam E &>/dev/null echo-e "Add \033[32m$username\033[0m successful" echo $Username | passwd--stdin $Username & >/dev/null ID $Usernamefi
5. Write a script that prompts the user to enter the value of the positive integer n to calculate the sum of the 1+2+3...+n
#!/bin/bash# #Sum =0read-p "input the int n:" ifor N in ' seq 1 $I ';d o//n fetch value from 11 until user input value I if [$N-le $I];then Let sum+= $N Fidoneecho "The Sum is $Sum"
6. Write a script that calculates the user input for several parameters and
#!/bin/bash# #Sum =0read-p "input the int n:" Ifor N in $I;d o//n values from the user input, using a method similar to [email protected] Let sum+= $Ndone echo-n "All the parameter Sum is:" Echo-n "' Echo $I | TR "" + "'"//change user input parameter blank to + echo-e "=\033[5;31m$sum\033[0m"
7. Write a script that prompts the user to enter several positive integers, then calculates the total product, and the last example of the user input parameters, such as: 1*3*10=30
#!/bin/bash#read-p "input some postive integer:" Integersum=1for K in $Integer;d oif [$K-le 0];thenecho "is wrong ~ "Exitfidonefor I in $Integer;d o sum=$ (($Sum * $I)) Done Echo-n" all the parameter Sum is: "Echo-n" ' Echo $Integer | TR "" "*" ' "ECHO-E" =\033[5;31m$sum\033[0m "
8. Find the number within 100 that can be divisible by 3, display 8 numbers per line, and then wrap the display
#!/bin/bash# #A =0for I in {1..100};d o while [$ ($I%3)-eq 0];d o echo-ne "$I \ T" let i++ let a++ The calculation satisfies the number of integers divisible by 3 if [$ ($A%8))-EQ 0];then//is divisible by 3 for multiples of 8 and wraps echo Fi Donedoneecho
9. Use the while loop to write the script so that it accomplishes the following functions
1. Prompt the user to enter two integers: Firstnum and Secondnum (fistnum value must be less than secondnum)
2. Output all the odd numbers between Firstnum and Secondnum
3. Output all the even numbers between Firstnum and Secondnum.
#!/bin/bash# #read -p "input 2 numbers,the first must less equal The second one: " First Secondwhile [ $First -gt $Second ];d o echo "The first one must less equal the second one, please input again :) " read -p " Input 2 numbers,the first must less equal the second one: " first seconddonea=$[$First +1] b=$[$Second -1] //the scope of the two to meet the problem in between the requirements of sum=0echo -n "The odd in the first number and second number is: ' for I in ' seq $A $B ';d o if [ $[$I%2] -eq 1 -o $[$I %2] -eq -1 ];then //to determine if I is odd echo -en "\033[5;32m$i\033[0m " //correct then output I value else let sum+ = $I //i value is an even number is accumulated fi doneechoecho -e "the sum of the even in the first Number and second one is:\033[5;31m$sum\033[0m "
10. Use only while to write a script that prompts the user to enter a positive integer (if a positive integer is not required for the user to reenter) and then outputs any prime numbers that are less than or equal to that number
#!/bin/bash# #read -p "input the number: " prime while [[ ! "$Prime" =~ ^[[:d igit:]]*$ | | $Prime -le 0 ]];d o //judgment user input as positive integer read -p "you need put a positive integer: " Prime doneI=1echo -n " the primes in $Prime is: "while [ $I -le $Prime ];d o K=1 N=0 J=2 while [ $J -lt $ (($I-1)) ];d o / The/j value starts at 2, and after 1 starts the Loop while [ $ (($I% $J)) cannot be entered -eq 0 -a $N -eq 0 ];d o //to determine whether the result of J modulo is 0 k=0 //record a return value, can not directly output I value, avoid multiple judgments multiple output n=1 //If the condition enters the loop, set a bounce value done let J++ done m=0 // Set the above n values to jump out of the loop while [ $K -eq 1 -a $M -eq 0 ];d o //to determine if the return value is the initial value echo -n $I //enters the above loop because it is not a prime number, it changes the return value to k=0 M=1 donelet i++doneecho
Note: Scripts are relatively simple when used with for and if, and interested students can write their own
11. Calculate how many days away from your birthday
#!/bin/bash# #read -p "Input your the month of yourbirthday,the format  SUCH AS 05&NBSP: " Birthmonth while [[ ! " $Birthmonth " =~ [0-9][0-9] | | $Birthmonth -lt 1 | | $Birthmonth -gt 12 ]];d o//to determine whether the user input two digits, single digits need zero complement full read -p "input two digits,the value must between in 01 and 12,such as 04 : " Birthmonth doneread -p " Input your the day of your birthday,the format such as 25 : " birthday until [[ $Birthday =~ [0-9][0-9] && $Birthday -gt 0 && $Birthday -le 31 ]];d o read -p "Input two digits,and the value must in 01 and 31,such as 25 : " birthday doneyear= ' date +%y ' //defining variables, simplifying subsequent references j= ' date +%s ' if [ $Birthmonth $birthday -eq ' date +%m%d ' ];then //if judgment if birthdays are equal to present time echo -e "\033[5;31mtoday is your birthday,happy birthday to you.\033[0m" elif [ $Birthmonth $birthday -gt ' date +%m%d ' ];then //if the birthday is not yet i= ' date -d $Year $birthmonth$birthday +%s ' echo -e ' \033[ 5;32m$ (((($I-$J))/60/60/24+1) \033[0m days later will be your birthday "Else //else birthday is over if [ $Birthmonth $birthday -eq 0229 ];then // If the birthday is 2/29th # case $ ((' date +%y '%4)) in //Judging if this year is a leap years 0) //If this is a leap year i= ' date -d $ (($Year +4)) $Birthmonth $birthday +%s ' echo -e "\033[5;35m$ (((($I-$J))/60/60/24+1) \033[0m days later will be your Birthday ";;       1) i= ' date -d $ (($Year +3)) $Birthmonth $birthday +%s ' echo -e "\033[5;35m$ (((($I-$J)/60/60/ 24+1)) \033[0m days later will be your birthday ";; 2) i= ' date -d $ ( $Year +2)) $Birthmonth $birthday +%s ' echo -e "\033[5;35m$ (((($I-$J)/60/60/24+1)) \033[0m days later will be your birthday ";; 3) i= ' date -d $ (($Year + 1)) $Birthmonth $birthday +%s ' echo -e "\033[5;35m$ ((((($I-$J))/60/60/24+1) \033[0m days later will be your birthday ";; esac else   //if birthdays are not 2/29th # i= ' date -d $ (($Year + 1)) $Birthmonth $birthday +%s ' echo -e "\033[5;35m$ ((((($I-$J))/60/60/24+1) \033[0m days later will be your birthday " fifi
Part of the Shell exercises (continuous update)