5、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)
1 #!/bin/bash
2 #
3 #
4 if [ $# -ne 1 ];then
5 echo "please input one parameter"
6 exit 3
7 fi
8
9 if [ -e $1 ];then
10 echo $(file $1)
11 else
12 mkdir -p $1
13 echo "make directory:"$1
14 fi
6、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)
1 #!/bin/bash
2 #
3 #
4 if ! [ $# -eq 0 ];then
5 if [ $# -lt 2 ];then
6 echo "At leaset two parameter input"
7 exit 3
8 fi
9 anum=$1
10 bnum=$2
11 if [ $anum -gt $bnum ];then
12 echo $anum">"$bnum
13 else
14 echo $bnum">"$anum
15 fi
16 else
17 read -p "please input number a:" anum
18 read -p "please input number b:" bnum
19 if [ $anum -gt $bnum ];then
20 echo $anum">"$bnum
21 else
22 echo $bnum">"$anum
23 fi
24
25 fi
7、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)
方法一:
1 #!/bin/bash
2 #
3 #
4 for i in {1..100};do
5 if ! [ $[$i%2] -eq 0 ];then
6 let sum+=$i
7 fi
8 done
9 echo $sum
10
11
方法二:
1 #!/bin/bash
2 #
3 #
4 i=1
5 while [ $i -le 100 ];do
6 if ! [ $[$i%2] -eq 0 ];then
7 let sum+=$i
8 fi
9 let i++
10 done
11 echo $sum
方法三:
1 #!/bin/bash
2 #
3 #
4 i=1
5 until [ $i -gt 100 ];do
6 if ! [ $[$i%2] -eq 0 ];then
7 let sum+=$i
8 fi
9 let i++
10 done
11 echo $sum
8、写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
1 #!/bin/bash
2 #
3 apath=$1
4 bpath=$2
5 anum=0
6 bnum=0
7
8 if [ $# -lt 2 ];then
9 echo "please input enough parameter"
10 exit 3
11 fi
12
13 anum=$(egrep "^$" $apath | wc -l)
14 bnum=$(egrep "^$" $bpath | wc -l)
15
16 if [ $anum -gt $bnum ];then
17 echo "MAX space line file is:"$apath" Total space line:"$anum
18 elif [ $anum -lt $bnum ];then
19 echo "MAX space line file is:"$bpath" Total space line:"$bnum
20 else
21 echo "The space line "$apath" = "$bpath" Total space line:"$bnum
22 fi
23
24 anum=0
25 bnum=0
26 anum=$(cat $apath| wc -l)
27 bnum=$(cat $bpath| wc -l)
28
29 if [ $anum -gt $bnum ];then
30 echo "MAX line file is:"$apath" Total line:"$anum
31
32 elif [ $anum -lt $bnum ];then
33 echo "MAX line file is:"$bpath" Total line:"$bnum
34 else
35 echo "The 2 file lines count is the same, Total line:"$bnum
36 fi
9、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:如果输入的是quit,则退出脚本;否则,则显示其输入的字符串内容;
1 #!/bin/bash
2 #
3 #
4 read -p "please input a char:" achar
5
6 if [ "$achar" == "quit" ];then
7 exit 3
8 else
9 echo $achar
10 fi
10、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
1 #!/bin/bash
2 #
3 #
4 if ! [ $# -eq 1 ];then
5 echo "please input one parameter"
6 exit 3
7 fi
8
9 results=0
10 chars=2
11
12 for i in $(seq 1 $1);do
13 if [ $i == "1" ];then
14 chars+="x1"
15 else
16 chars+="x2"
17 fi
18 results=$[2**$i]
19 echo $chars"="$results
20 if [ $i == "1" ];then
21 chars=2
22 fi
23 done
11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。
1 #!/bin/bash
2 #
3 #求两数之和
4 function two_num_count(){
5 read -p "please input two num:" a b
6
7 if [ -n "$a" -a -n "$b" ];then
8 counts=$[$a+$b]
9 echo $counts
10 return 3
11 else
12 echo "not enough parameter"
13 exit 22
14 fi
15 }
16
17
18 #求两数最大公约数
19 function maxx(){
20 local a
21 local b
22 local c
23 local aold
24 local bold
25
26 read -p "please input two num:" a b
27 if [ -n "$a" -a -n "$b" ];then
28 aold=$a
29 bold=$b
30 c=$[$a%$b]
31 while [ $c -ne 0 ];do
32 a=$b
33 b=$c
34 c=$[$a%$b]
35 done
36 echo $aold" and "$bold" GCD is:"$b
37 else
38 echo "not enough parameter"
39 exit 22
40 fi
41 }
42
43 #求两数最小公倍数
44 function minx(){
45 #最小公倍数=axb/ab的最大公约数
46 #求最大公约数开始
47 local a
48 local b
49 local aold
50 local bold
51 local c
52 local c1
53
54 read -p "please input two num:" a b
55 if [ -n "$a" -a -n "$b" ];then
56 aold=$a
57 bold=$b
58 c=$[$a%$b]
59 while [ $c -ne 0 ];do
60 a=$b
61 b=$c
62 c=$[$a%$b]
63 done
64 #求最大公约数结束
65 #求最小公倍数开始
66 c1=$[$aold*$bold/$b]
67 echo $aold" and "$bold" LCM is:"$c1
68 #求最小公倍数结束
69 else
70 echo "not enough parameter"
71 exit 22
72 fi
73
74 }
75
76
77 echo "please input num to select function:"
78 echo "1.两数值求和(Two number sum)"
79 echo "2.求两数最大公约数(GCD)"
80 echo "3.求两数最小公倍数(LCM)"
81 read -p ":" snum
82
83 if [ -n $snum ];then
84 case $snum in
85 1)
86 two_num_count
87 ;;
88 2)
89 maxx
90 ;;
91 3)
92 minx
93 ;;
94 *)
95 echo "Please select the correct options"
96 exit 22
97 esac
98 else
99 echo "please choose"
100 exit 22
101 fi