(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#if [$#-lt 1]; Then echo "Plz Enter a path:" Exit 1fi if [-e $]; Then file $1else mkdir-p $1fi ~
Test script:
[[email protected] bin]# Bash Test20.shplz Enter a path:[[email protected] bin]# bash-x test20.sh abc+ ' [' 1-lt 1 '] ' + ' ['-e abc '] ' + mkdir-p abc[[email protected] bin]# bash-x test20.sh abc+ ' [' 1-lt 1 '] ' + ' ['-e-ABC '] ' + file Abcabc: Directory
(2) Write a script to complete the following functions, to determine the given two values, whichever is smaller; the method of the given number: script parameter, command interaction;
#!/bin/bash#if (($# < 2)); Then echo "Plz enter Numbers!" Exit 1fi if (($ > $)); Then echo "$ > $" elif (($ < $)); Then echo "$ < $" Elif (($ = = $)); Then echo "$ = $" else echo "disparate" fi~
Test scripts
[Email protected] bin]# bash test21.shplz enter the integer numbers! [[email protected] bin]# Bash test21.sh 4Plz enter the integer numbers! [[email protected] bin]# Bash test21.sh 4 < 5[[email protected] bin]# bash test21.sh 5 = 5[[email protected] bin] # bash test21.sh 5 > 4
(3) the sum of all odd numbers within the range (at least 3 methods)
use the For
#!/bin/bash#for i in $ (seq 100); do if [$[$i%2]-ne 0]; Then h=$[$h + $i] fidone echo "sum: $h"
Test script:
[Email protected] bin]# bash test22.shsum:2500
2. Using the While
#!/bin/bash#h=1j=0while [$h-le 100]; do if [$[$h%2]-ne 0]; Then j=$[$h + $j] fi leth++doneecho $j
Test script:
[Email protected] bin]# bash test23.sh2500
3. temporarily unexpected
(4) write a script to achieve 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;
#!/bin/bash# if [ $# -lt 2 ]; then echo "Plz enter two path: " exit 1fi if [ ! -f $1 ]; then echo "$1 not a common file!" exit 1fiif [ ! -f $2 ]; then echo "$2 not a common file! " exit 1fij=$ (grep -c "^$" $1) h=$ (grep -c "^$" $2) m=$ (cat $1 |wc -l) n=$ (cat $2 |wc -l) if [ $j -ge $h ]; then echo "$1 is max ,the spaceline total: $j" else echo "$ is max, the spaceline total: $h "fiif [ $m -ge $n ]; then echo "$1 is max, the lines total: $m" else echo "$2 is max,the lines tOtal: $n "fi
Test script:
[[email protected] bin]# bash -x test24.sh/etc/fstab /etc/yum.conf+ ' [' 2 -lt 2 ' ' + ' [' '! ' -f /etc/fstab ' ' + ' [' '! ' -f /etc/yum.conf ' ++ grep -c ' ^$ ' /etc/fstab+ j=1++ grep -c ' ^$ ' &NBSP;/ETC/YUM.CONF+&NBSP;H=3++&NBSP;WC&NBSP;-L++&NBSP;CAT&NBSP;/ETC/FSTAB+&NBSP;M=10++&NBSP;WC -l++ cat /etc/yum.conf+ n=26+ ' [' 1 -ge 3 '] ' + echo '/ Etc/yum.conf is max, the spacelinetotal:3 '/etc/yum.conf is max, the spaceline total:3+ ' [' 10 -ge 26 '] ' + echo '/etc/yum.conf is max,the linestotal:26 '/etc/yum.conf is max,the lines total:26[[email protected] bin]# bash test24.shplz enter two path:[[email protected] Bin]# bash test24.sh abcplz enter two path:[[email protected] bin]# bash test24.sh abc cbaabc not a common file!
(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 "Plz input a string:"-t5 AIF [-Z $a]; Then echo "Plz enter string!" Exit 1fiif [[$a = = "quit"]]; Then Exit 1else echo "$a" fi
Test script:
[[email protected] bin]# bash test25.shplz input a string:abcabc[[email protected] bin]# bash test25.shplz input a string: Quit[[email protected] bin]#[[email protected] bin]# bash test25.shplz input a string:plz enter string!
write a script to print the 2^n table;n equals a user-entered value
Shell Script Job