linux shell编程

linux shell编程

1)撰写一个 script ,完成让使用者输入:1. first name 与 2. last name,最后幵且在屏幕上显 示:Your full name is: 的内容。

1 #!/bin/bash
2 #Program:
3 #        User inputs his first name and last name. Program shows his full name.
4 read -p "Please input your first name: " firstname
5 read -p "Please input your last name: " lastname
6 echo -e "\nYour full name is:$firstname $lastname"

2)撰写一个script(用[]测试指令来完成),完成如下功能:1. 当执行一个程序的时候,这个程序会让用户选择 Y 或 N , 2. 如果用户输入 Y 或 y 时,就显示『 OK, continue 』 3. 如果用户输入 n 或N 时,就显示『 Oh, interrupt !』 4. 如果不是 Y/y/N/n 以内的其他字符,就显示『 I don't know what your choice is 』

1 #!/bin/bash
2 #Program:
3 #       This program will show the user's choice
4 read -p "Please input (Y/N): " yn
5 [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue" && exit 0
6 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh,interrupt!" && exit 0
7 echo "I don't know what is your choice" && exit 0

 

3)将上一个程序用if...then...的方法来完成。

1 #!/bin/bash
2 read -p "Please input (Y/N):" yn
3 if [ "$yn" == "Y" -o "$yn" == "y" ]; then
4 echo "OK,continue"
5 elif [ "$yn" == "N" -o "$yn" == "n" ]; then
6 echo "Oh,interrupt!"
7 else
8 echo "I don't know what is your choice"
9 fi

4)分析下面脚本程序的功能:

#!/bin/bash

#提示用户输入

read -p "please input a directory:" dir 

#判断文件是否存在,若不存在则显示信息并结束脚本    

if [ "$dir == "" -o ! -d "$dir" ];then    

echo "the $dir is not exit in your system."

      exit 1

fii

filelist=$(ls $dir)

for filename in $filelist

do

      perm=""                               

#开始判断文件类型和属性  

      test -r "$dir/$filename" && perm="$perm readable"

      test -w "$dir/$filename" && perm="$perm writable"

      test -x "$dir/$filename" && perm="$perm executable"

#开始输出信息!

      echo "the file $dir/$filename's permission is $perm" 

done

 1 #!/bin/bash
 2 #提示用户输入
 3 read -p "Please input a directory: " dir
 4 #判断文件是否存在,若不存在则显示信息并结束脚本
 5 if [ "$dir" = ""  -o ! -d "$dir" ]; then
 6     echo "the $dir is not exit in your system"
 7     exit 1
 8 fi
 9 filelist = $(ls $dir)
10 for filename is $filelist
11 do
12    perm = ""
13 #开始判断文件类型和属性
14    test -r "$dir/$filename" && perm = "$perm readable"
15    test -w "$dir/$filename" && perm = "$perm writable"
16    test -x "$dir/$filename" && perm = "$perm executable"
17 #开始输出信息
18    echo "the file $dir/$filename's permission is $perm"
19 done

posted @ 2015-05-11 18:05  竹立荷塘  阅读(363)  评论(0编辑  收藏  举报