Linux shell Programming
1) Write a script to complete the user input: 1. First name with 2. Last name, finally shipowner and displayed on the screen: Your full name is: content.
1#!/bin/Bash2 #Program:3 # User inputs his first name and last name. Program shows he full name.4Read-p"Please input your first name:"FirstName5Read-p"Please input your last name:"LastName6Echo-e"\nyour full name is: $firstname $lastname"
2) Write a script (complete with [] test instructions) to complete the following functions: 1. When executing a program, this program will let the user choose Y or N, 2. If the user enters Y or y, the "OK, continue" 3 is displayed. If the user enters n or n, it displays "Oh, Interrupt!" "4. If it is not the other characters within the y/y/n/n, it shows" I don t know what your choice is "
1#!/bin/Bash2 #Program:3# This program would show the user's Choice4Read-p"Please input (y/n):"yn5["$yn"=="Y"-O"$yn"=="y"] && Echo"ok,continue"&& exit06["$yn"=="N"-O"$yn"=="N"] && Echo"oh,interrupt!"&& exit07Echo"I don ' t know what's your choice"&& exit0
3) The previous program with If...then ... method to complete.
1#!/bin/Bash2Read-p"Please input (y/n):"yn3 if["$yn"=="Y"-O"$yn"=="y" ]; Then4Echo"ok,continue"5elif ["$yn"=="N"-O"$yn"=="N" ]; Then6Echo"oh,interrupt!"7 Else8Echo"I don ' t know what's your choice"9Fi
4) Analyze the features of the following script:
#! /bin/bash
#提示用户输入
Read-p "Please input a directory:" dir
#判断文件是否存在, displays the message and ends the script if it does not exist
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/Bash2 #提示用户输入3Read-p"Please input a directory:"dir4 #判断文件是否存在, displays the message and ends the script if it does not exist5 if["$dir"=""-O! -D"$dir" ]; Then6Echo"The $dir is not a exit in your system"7Exit18 fi9FileList =$ (ls $dir)Ten forFileName is$filelist One Do APerm ="" - #开始判断文件类型和属性 -Test-r"$dir/$filename"&& perm ="$perm readable" theTest-w"$dir/$filename"&& perm ="$perm writable" -Test-x"$dir/$filename"&& perm ="$perm Executable" - #开始输出信息 -Echo"The file $dir/$filename ' s permission is $perm" +Done
Linux shell Programming