<>Linux shell Summary of script exercises

*
Write script file example1.sh, Store in /home/cauc/scripts Under the directory . The script function is ： Find out if a file exists （ In parameter form ）, If present , Set its permission as user readable and modifiable , Others have no authority . Otherwise, the information without this file is returned .
#!/bin/bash if [ -e \$1 ] then chmod 600 \$1 else echo "The file \$1 does not
exist" fi
* Write script file leapyear.sh, Store in /home/cauc/scripts Under the directory . The script function is ： Judge whether a given year is a leap year . #!/bin/bash
echo "Input a year number:" read year let "leap=\$year%4==0&&\$year%100!=0||\$year
%400==0" if [ \$leap -eq 0 ] then echo "\$year is not a leap year" else echo "
\$year is a leap year" fi
* Prints the of a given number of lines * number . First line print 1 individual , Second line print 2 individual , wait . The number of lines is entered by the user on the command line . #!/bin/bash for((i=0;i<\$1;i++))
do for((j=0;j<=i;j++)) do echo -n "*" done echo "" done
* Write script file sum.sh, Store in /home/cauc/scripts Under the directory . The script function is ： Calculate from 1 Add 100 And returns the result . #!/bin/bash s=
0 for((i=0;i<=100;i++)) do s=\$((\$s+\$i)); done echo "the sum is:\$s"
* to write shell script , Parameter is greater than 20 Positive integer of . First check whether the parameters meet the requirements . If not
Meet the requirements , Please give a hint ; If it meets the requirements , Output the square of this parameter . #!/bin/bash if [ \$1 -gt 20 ] then echo "\$((\$1*\$1
))" else echo "number is wrong" fi
* to write shell script , The date of the day is displayed first , Then find out whether the given user is working in the system （who command ）. If in the system , Just output a welcome statement （ for example hello,
****!）; If not in the system , Just output a statement （waiting for ***!） #!/bin/bash date if who|grep "^\$1" then
echo "hello,\$1!" else echo "waiting for \$1!" fi
* to write shell script , The script accepts a parameter . If the given parameter is not a directory , Then a prompt message will be given ;
Otherwise use ll The command lists the contents of the directory , And output how many subdirectories there are （d start ）, How many ordinary files (- start ）. #!/bin/bash if [ -d \$1 ]
then ls -l \$1 echo -n "d:" ls -l \$1|grep "^d"|wc -l echo -n "-:" ls -l \$1|grep
"^-"|wc -l else echo "\$1 does not existence" fi
* to write shell script , The content specified by the first parameter copy To the location specified by the second parameter .
If the first parameter is directory , Auto add -r option （ That is, all the contents in the directory are deleted copy past times ）;
If the first parameter is an ordinary file , Then copy To the designated place ;
If the file or directory specified by the first parameter does not exist , An error is reported ;
If the file or directory specified by the second parameter already exists , Prompt whether to replace , If selected yes, Delete the original file or directory first , Then execute copy operation , Otherwise give up .
#!/bin/bash if [ -e \$1 ] then if [ -e \$2 ] then echo "replace?[yes/no]" read ans
if [ \$ans=="yes" ] then if [ -d \$1 ] then cp -r \$1 \$2 else cp \$1 \$2 fi fi else
if [ -d \$1 ] then cp -r \$1 \$2 else cp \$1 \$2 fi fi else echo "error!" fi
* to write shell script . Check whether the first parameter on the command line is -b perhaps -s. If it is -b, Calculated by
In the file specified by the second parameter b Number of lines at the beginning . If it is -s, Is calculated in the file specified by the second parameter
with s Number of lines at the beginning . Otherwise, the information with wrong selection will be displayed . #!/bin/bash case \$1 in -b) count = `grep ^b \$2 | wc -l
` echo “The number of lines in \$2 that start with b is \$count.”;; -s) count = `
grep ^s \$2 | wc -l` echo “The number of lines in \$2 that start with s is \$count
.”;; *) echo “Theoption is not recognized”;; esac
* to write shell script . The script needs to enter the names of two files , Then the user selects the corresponding operation （ if
Either of the two parameters is not an ordinary file , An error is reported ）：
cat： Output the contents of two files ;
statistic： Count the number of lines in two files
merge： Will be the first 1 Merge the contents of the first file into the second 2 After a file ;
copy： Will be the first 1 Contents of files copy To the first 2 Files （ The first 2 Original contents of files are overwritten ）;
bye： sign out . #!/bin/bash if [[ -f \$1 && -f \$2 ]] then echo “please chose your cat,
statistic, merge, copy, or bye.”read chose case \$chose in cat) cat \$1 \$2;;
statistic) echo `wc -l \$1` echo `wc -l \$2`;; merge) cat \$1 >> \$2;; copy) cat \$1
> \$2;; bye) break;; esac else echo "\$1 or \$2 is not file" fi
* to write shell script , utilize for Loop to the current directory .c Move the file to the specified directory , complete
Display the contents of the file under the specified directory , And sort the documents from small to large . (ll -r -S） #!/bin/bash for i in `ls|grep -E "*.c"`
do mv \$i \$1 done ls -l -r -S \$1
* to write shell script , display Fibonacci Front of sequence 10 Item and its sum . #!/bin/bash a=1 b=1 sum=2 cnt=4 echo
-n -e"\$a\t\$b" while [ \$cnt -gt 0 ] do let "a=a+b" let "b=a+b" let "sum=sum+a+b"
let "cnt=cnt-1" echo -n -e "\t\$a\t\$b" done echo "" echo "sum=\$sum"
* to write shell script , Judge whether the given parameter is prime .( Assume greater than 2） #!/bin/bash flag=0 if for (( i=2; i<=\$1/2;i
++ )) do if ((\$1%i==0)) then flag=1 break fi done if [ flag -eq 1 ] then echo \$1
is not a prime numberelse echo \$1 is a prime number fi
14) to write shell script , Converts a given parameter to a binary representation .
#!/bin/bash a=0 b=\$1 c=1 sum=0 while [ \$b -gt 0 ] do let "a=b%2" let "b=b/2"
let "sum=sum+a*c" let "c=c*10" done echo "\$sum"
* Suppose a folder exists ( As a parameter ）, Including one studentlist.csv Documents , When
The student numbers of several students are stored in the , Each line . for example ：
150341101
150341102
150341105
150341106
to write shell script , see /homework Did the student submit the homework under the folder , Assume that the format of the job name is ： Student number _homework.txt. Finally, output the list of student numbers that have not submitted their homework .
#!/bin/bash cd \$1 for i in `cat studentlist.csv` do str="_homework.txt" file=\$i
\$str if [ -f \$file ] then echo "\$i yes" else echo "\$i no" fi done