Chinaunix首页 | 论坛 | 博客
  • 博客访问: 803982
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2011-12-22 22:04:00

1.  编写一个接收一个文件名的shell脚本。如果此文件为一般文件,该脚本应显示以下消息:

输入文件名 is an ordinary file display?

如果回答是’y,且该文件有读许可的话应显示文件内容,否则,该脚本应显示以下消息且终止:

Sorry, 输入文件名 has no read permission.

如果输入的是一个目录文件,确认该文件存在且有许可后,应显示该目录中的所有文件列表。

如果输入的不是文件或目录,则显示相应的错误信息。

#!/bin/bash

 

if [ -e $1 ]; then

   echo "$1 is an ordinary file   display?"

else

   echo "$1 is not an ordinary file"

   exit 0

fi

 

read res

 

  if [ $res = 'y' ]; then

      if [ -f $1 -a -r $1 ];then

        cat $1

      else

        ls $1

      fi

   else

       echo "sorry $1 has no read permission"

       exit 0

   Fi

 

 

2. 编写一个产生以下序列的shell脚本:

13243546..100

#!/bin/bash

 

num1=1

num2=3

 

while [ $num2 -le 100 ]

do

  echo -n "$num1, $num2, "

  num1=$(($num1+1))

  num2=$(($num2+1))

done

Echo

 

3. 编写一个shell脚本,检查输入的字符串是否为palindrome

#!/bin/bash

 

read str

if [ $str = 'palindrome' ]

then

  echo "true"

else

  echo "faulse"

Fi

 

4. 编写一个显示以下模式的shell脚本,接收一个数字,使用while循环:(1打印1次,2打印2次,3打印3次。。。)

1

   22

   333

   4444

   。。。。。

#!/bin/bash

 

read line

num=1

 

while [ $num -le $line ]

do

  temp=$num

  while [ $temp -ge 1 ]

  do

    echo -n $num

    temp=$(($temp-1))

  done

echo

num=$(($num+1))

Done

 

5. 编写一个shell脚本,打印任何数的乘法表。

如:

输入一个数2

打印:

2 * 1 = 2

2 * 2 = 4

2 * 3 = 6

..

2 * 10 = 20

#!/bin/bash

 

read num

temp=1

 

while [ $temp -le 10 ]

do

   echo "${num}*${temp}=$((${num}*${temp}))"

   temp=$(($temp+1))

Done

 

6. 编写一个脚本,显示以下序列:

1 2 3 4 10 5 6 7 8 26 9 10 11 12 42 13 14 15 16 58 17 18 19 20 74

#!/bin/bash

 

num=1

i=0

 

while [ $num -le 20 ]

do

    echo -n "$num "

    arry[$i]=$num

 

    if [ $(($num % 4)) -eq 0 ]; then

        echo -n "`expr ${arry[0]} + ${arry[1]} + ${arry[2]} + ${arry[3]}` "

    fi

 

    num=`expr $num + 1`

    i=`expr $i + 1`

 

    if [ $i -eq 4 ]; then

       i=0

    fi

done

echo

阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~