首页 > 系统服务 > 详细

shell脚本作业

时间:2015-09-25 03:57:34      阅读:273      评论:0      收藏:0      [点我收藏+]

写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型

#!/bin/bash

#

if [ $# -lt 1 ]; then

echo "Plz enter a path:"

exit 1

fi

if [ -e $1 ]; then

file $1

else

mkdir -p $1

fi

~

测试脚本:

[root@www bin]# bash test20.sh

Plz enter a path:

[root@www bin]# bash -x test20.sh abc

+ ‘[‘ 1 -lt 1 ‘]‘

+ ‘[‘ -e abc ‘]‘

+ mkdir -p abc

[root@www bin]# bash -x test20.sh abc

+ ‘[‘ 1 -lt 1 ‘]‘

+ ‘[‘ -e abc ‘]‘

+ file abc

abc: directory

写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;

#!/bin/bash

#

if (($# < 2)); then

echo "Plz enter Two integer Numbers!"

exit 1

fi

if (($1 > $2)); then

echo "$1 > $2"

elif

(($1 < $2)); then

echo "$1 < $2"

elif

(($1 == $2)); then

echo "$1 = $2"

else

echo "disparate"

fi

~

测试脚本

[root@www bin]# bash test21.sh

Plz enter Two integer Numbers!

[root@www bin]# bash test21.sh 4

Plz enter Two integer Numbers!

[root@www bin]# bash test21.sh 4 5

4 < 5

[root@www bin]# bash test21.sh 5 5

5 = 5

[root@www bin]# bash test21.sh 5 4

5 > 4

求100以内所有奇数之和(至少用3种方法)

  1. 使用for

#!/bin/bash

#

for i in $(seq 100); do

if [ $[$i%2] -ne 0 ]; then

h=$[$h+$i]

fi

done

echo "sum:$h"

测试脚本:

[root@www bin]# bash test22.sh

sum:2500

2.使用while

#!/bin/bash

#

h=1

j=0

while [ $h -le 100 ]; do

if [ $[$h%2] -ne 0 ]; then

j=$[$h+$j]

fi

let h++

done

echo $j

测试脚本:

[root@www bin]# bash test23.sh

2500

3.暂时想不到

写一个脚本实现如下功能:

(1) 传递两个文本文件路径给脚本;

(2) 显示两个文件中空白行数较多的文件及其空白行的个数;

(3) 显示两个文件中总行数较多的文件及其总行数

#!/bin/bash

#

if [ $# -lt 2 ]; then

echo "Plz enter Two path:"

exit 1

fi

if [ ! -f $1 ]; then

echo "$1 not a common file!"

exit 1

fi

if [ ! -f $2 ]; then

echo "$2 not a common file!"

exit 1

fi

j=$(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 "$2 is max, the spaceline total:$h"

fi

if [ $m -ge $n ]; then

echo "$1 is max, the lines total:$m"

else

echo "$2 is max,the lines total:$n"

fi

测试脚本:

[root@www 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 ‘^$‘ /etc/yum.conf

+ h=3

++ wc -l

++ cat /etc/fstab

+ m=10

++ wc -l

++ cat /etc/yum.conf

+ n=26

+ ‘[‘ 1 -ge 3 ‘]‘

+ echo ‘/etc/yum.conf is max, the spaceline total:3‘

/etc/yum.conf is max, the spaceline total:3

+ ‘[‘ 10 -ge 26 ‘]‘

+ echo ‘/etc/yum.conf is max,the lines total:26‘

/etc/yum.conf is max,the lines total:26

[root@www bin]# bash test24.sh

Plz enter Two path:

[root@www bin]# bash test24.sh abc

Plz enter Two path:

[root@www bin]# bash test24.sh abc cba

abc not a common file!

9、写一个脚本

(1) 提示用户输入一个字符串;

(2) 判断:

如果输入的是quit,则退出脚本;

否则,则显示其输入的字符串内容

#!/bin/bash

#

read -p "Plz input a string:" -t 5 a

if [ -z $a ]; then

echo "Plz enter string!"

exit 1

fi

if [[ $a == "quit" ]]; then

exit 1

else

echo "$a"

fi

测试脚本:

[root@www bin]# bash test25.sh

Plz input a string:abc

abc

[root@www bin]# bash test25.sh

Plz input a string:quit

[root@www bin]#

[root@www bin]# bash test25.sh

Plz input a string:Plz enter string!

写一个脚本,打印2^n表;n等于一个用户输入的值

shell脚本作业

原文:http://8162402.blog.51cto.com/8152402/1698023

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!