1. Write a script: If a path does not exist, it is created as a directory, otherwise it is present, and the content type is displayed;
#!/bin/bash path= "/tmp/a/b/fang" if [-e $path]; Then echo "$path exists!" File $path else mkdir-p $path fi
2, Write a script that does the following, judging by the size of the given two values, the method of the given number: script arguments, command interaction;
#!/bin/bash read-p "Please input number:"-t10 num1 num2 if [-z] $num 1 "]&&[-Z" $num 2 "]; Then echo "Give numbers." Exit 1 fi if [$num 1-ge $num 2]; Then echo "Max: $num 1 min: $num 2" Else echo "Max: $num 2 MIN: $num 1" fi
3. all the odd sum within 100
Method 1:
#!/bin/bash declare-i sum=0 for i in $ (SEQ 0 2 100); Do sum=$ (($sum + $i)) did echo "even sum: $sum."
Method 2:
#!/bin/bash declare-i sum=0 for i in {1..100}; doif [$[$i%2]-eq 0]; thensum=$[$sum + $i]fi done echo "even sum: $sum."
Method 3:
#!/bin/bash declare-i sum=0 Add () {for I in $ (SEQ 0 2), do sum=$[$sum +i] done} add echo "The sum is $sum."
4. write a script to implement the following functions:
(1) Pass two text file path to script;
(2) Show the number of blank lines in two files and their blank lines;
(3) Two files with a larger total number of rows and their total rows are displayed;
#!/bin/bash text1=sedawkprocess0915.txt text2=sedawkprocess0917.txt tj1= ' grep ' ^$ ' sedawkprocess0915.txt | wc -l ' tj2= ' grep ' ^$ ' Sedawkprocess0917.txt | wc -l ' total1= ' cat Sedawkprocess0915.txt | wc -l ' total2= ' cat sedawkprocess0917.txt | wc -l ' if [ $tj 1 -gt $tj 2 ]; then echo "$text 1 total blank lines are $ Tj1 " else echo " $text 2 total blank lines are $TJ 2 " fi if [ $ total1 -gt $total 2 ]; then echo "$text 1 total lines are $total 1 " else echo " $text 2 total lines are $ Total2 " fi
5. Write a script
(1) Prompt the user to enter a string;
(2) judgment:
If the input is quit, exit the script;
Otherwise, the string content of its input is displayed;
#!/bin/bash read-p "Please input a string:"-t10 string if [$string = = "Quit"]; Then exit 0 Else echo ' This string is $string. ' Fi
6. write a script to print the 2^n table; n equals a user-entered value;
#!/bin/bash read-p "Please input a number:"-t10 number i=0 while [$i-lt];d o i=$[$i +1] Echo-n -E "2^ $i =$[2** $i]" echo done
7, write a script, write a few functions: function 1, achieve the sum of two values given, function 2, take the greatest common divisor of a given two value, function 3, The least common multiple of the given two numeric values, and the size of the selected two values for the function is provided through interactive input.
#!/bin/bash if [[ $# -le 2 ]];then echo "plz input two number and use space to isolation them! " fi function add () { echo "the two num sum:$1+$2= ' expr $1 + $2 '" } add $1 $2 declare -i big declare -i small if [ $1 -gt $2 ];then big=$1 small=$2 else big=$2 small=$1 fi       GCDLCM () { i=1 # Define a loop variable GCD=1 #定义最大公约数 LCM=1 #定义最小公倍数 btmp=1 #定义用户输入的较大的一个变量除以循环变量的值 stmp=1 #定义用户输入的较小的一个变量除以循环变量的值 while [ $i -le $small ];d o #定义循环条件, the loop variable is less than or equal to the smaller variable entered by the user btmp= ' expr $big % $i ' #求值 stmp= ' expr $small % $i ' #求值 if [ $btmp -eq 0 ];then #判断值得余数是否为0 if [ $stmp -eq 0 ];then #同上 gcd= $i # If the remainder is worth 0, get the value of the maximum number of Conventions fi #判断结束 fi i= ' expr $i + 1 ' #i变量循环 until I equals a smaller number of user input to exit the loop. done #当i = $sma, exit loop lcm= ' expr $small / $gcd ' #根据最小公倍数公式求值 lcm= ' expr $lcm \* $big ' #同上, least common multiple formula: lcm= $sma * $big%gcd echo "LCM: $LCM" # Output LCM Value echo "GCD: $gcd" #输出gcd值 }
September 15 Jobs