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

转载于:https://www.cnblogs.com/daipianpian/p/4495236.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值