|
一、shell程序的运行原理和知识点
二、循环语句、条件判断的使用方法及其相关示例
三、文本处理工具sed及awk的用法
练习题
1、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)
方法一
#!/bin/bash
#
filename="/tmp/x/y/z/testdir"
if [ -e $filename ]; then
echo "$filename exists."
file $filename
else
mkdir -p $filename
fi
[root@centos6 tmp]# bash -x 3.sh
+ filename=/tmp/x/y/z/testdir
+ '[' -e /tmp/x/y/z/testdir ']'
+ mkdir -p /tmp/x/y/z/testdir
[root@centos6 tmp]# tree /tmp
/tmp
├── 1.sh
├── 2.sh
├── 3.sh
├── backup
├── lost+found
├── x
│ └── y
│ └── z
│ └── testdir
方法二
[root@centos6 ~]# vi 7.sh
#!/bin/bash
#
if [ -e $1 ];then
echo "$! is exist"
file $1
else
mkdir -p $1
fi
[root@centos6 ~]# chmod +x 7.sh [root@centos6 ~]# bash -n 7.sh [root@centos6 ~]# ./7.sh /tmp/a/b[root@centos6 tmp]# tree.├── 1.sh├── 2.sh├── 3.sh├── 4.sh├── 5.sh├── 6.sh├── a│ └── b├── backup├── lost+found├── x│ └── y│ └── z│ └── testdir├── yum.log├── yum_save_tx-2015-09-07-16-32XZwR1d.yumtx└── yum_save_tx-2015-09-13-15-46Ii5S8b.yumtx
2、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)
方法一
#!/bin/bash
#
read -p "Plz enter two integer: " -t 10 num1 num2
if [ -z "$num1" ]; then
echo "Plz give two integers."
exit 1
fi
if [ -z "$num2" ]; then
echo "Plz give tow integers."
exit 1
fi
if [ $num1 -ge $num2 ]; then
echo "Max: $num1, Min: $num2."
else
echo "Max: $num2, Min: $num1."
fi
[root@centos6 tmp]# ./6.sh Plz enter two integer: 3 6Max: 6, Min: 3.
方法二
[root@centos6 ~]# vi 8.sh
#!/bin/bash
#
if [ $1 -gt $2 ];then
echo "Max:$1,Min:$2."
else
echo "Max:$2,Min$1."
fi
[root@centos6 ~]# chmod +x 8.sh [root@centos6 ~]# ./8.sh 5 7Max:7,Min5.
3、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)
方法一
[root@centos6 ~]# vi 9.sh
#!/bin/bash
#
declare -i sum=0
for i in $(seq 1 2 100); do
sum=$(($sum+$i))
done
echo "Even sum: $sum."
[root@centos6 ~]# bash 9.sh
Even sum: 2500.
方法二
[root@centos6 ~]# vi 10.sh
#!/bin/bash
#
declare -i sum=0
for i in {1..100}; do
if [ $[$i%2] -eq 1 ]; then
sum=$[$sum+$i]
fi
done
echo "Even sum: $sum."
[root@centos6 ~]# bash 10.sh
Even sum: 2500.
方法三
[root@centos6 ~]# vi 10.sh
#!/bin/bash
i=1
sum=0
until [ $i -gt 100 ]
do
sum=$[$sum+$i]
i=$[$i+2]
done
echo "Even sum: $sum."
[root@centos6 ~]# bash 11.sh
Even sum: 2500.
4、写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
#!/bin/bash
read -p "Please input two file_path:" -t 90 file1 file2
all_line1=`wc -l $file1 |cut -d' ' -f1`
all_line2=`wc -l $file2 |cut -d' ' -f1`
space_line1=`grep "^$" $file1 | wc -l |cut -d' ' -f1`
space_line2=`grep "^$" $file2 | wc -l |cut -d' ' -f1`
if [ $space_line1 -gt $space_line2 ];then
echo "The $file1 more space lines. space_line Counts: $space_line1 Lines."
else
echo "The $file2 more space lines. space_line Counts: $space_line2 Lines."
fi
if [ $all_line1 -gt $all_line2 ];then
echo "The $file1 more lines. line.Counts: $all_line1 Lines."
else
echo "The $file2 more lines . line.Counts: $all_line2 Lines."
fi
[root@svn ~]# bash 4.sh
Please input two file_path:/etc/passwd /etc/fstab
The /etc/fstab more space lines. space_line Counts: 1 Lines.
The /etc/passwd more lines. line.Counts: 23 Lines.
5、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容;
#!/bin/bash
read -p "Please Enter a String: " str
if [ $str = "quit" ];then
exit
else
echo "$str"
fi
[root@svn ~]# bash 5.sh
Please Enter a String: ludghfgj
ludghfgj
[root@svn ~]# bash 5.sh
Please Enter a String: quit
6、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
#!/bin/bash
read -p "Please Enter the N: " n
for i in $(seq 0 $n)
do
echo "2^$i=$[2**$i]"
done
[root@svn ~]# bash 6.sh
Please Enter the N: 8
2^0=1
2^1=2
2^2=4
2^3=8
2^4=16
2^5=32
2^6=64
2^7=128
2^8=256
[root@svn ~]# bash 6.sh
Please Enter the N: 3
2^0=1
2^1=2
2^2=4
2^3=8
7、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。
还没有想出来,还得多看几遍视频和其他资料后补充
|
|
|