Chapter 1 judgment & Chapter 2 exercise

Source: Internet
Author: User

1. Compile a program named rename, rename the file given by the 1st parameters, and add a string of characters contained in the 2nd parameters after the original name.
Rename memol. sv
Change the file memol to memol. sv.
// Key:
[Root @ localhost ~] # Cat Test
MV $1 $1 $2
[Root @ localhost ~] # Chmod + X test
[Root @ localhost ~] # Ls
Nu
[Root @ localhost ~] #./Test Nu. txt
[Root @ localhost ~] # Ls
Nu.txt
[Root @ localhost ~] #
2. Compile a program named unrename. Remove the strings contained in the 1st parameters from the back of the file name specified by the 2nd parameters, that is
Unrename memol. sv. sv
The file memol. sv should be renamed to memol. Ensure that the character string is removed from the end, such
Unrename test1test Test
Tes1test should be renamed to test1 (Note: Use SED and command to replace)
// Key:
MV $1 $1 | SED's/$2 // 'removes the place where the real parameter 1 contains the real parameter 2.

[Soflib @ localhost ~] $ Cat key2
Echo "$1"
#$1 | SED's/$2 //'
Del = $ (echo $1 | sed "s/$2 //")
Echo $ del
MV $1 $ del
[Soflib @ localhost ~] $

Chapter 1 judgment
P4
[Root @ localhost test01] # Cat on
User = "$1"
If who | grep "^ $ user"
Then
Echo "$ user is logged on"
Fi
[Root @ localhost test01] # chmod + X on
[Root @ localhost test01] #./on Root
Root tty1 2010-04-22 14: 46 (: 0)
Root pts/0 2010-04-22 14: 54 (: 0.0)
Root is logged on
[Root @ localhost test01] #

P8
[Root @ localhost test01] # X1 = "005"
[Root @ localhost test01] # ["$ X1" = 5]
[Root @ localhost test01] # echo $?
1
[Root @ localhost test01] # ["$ X1"-EQ 5]
[Root @ localhost test01] # echo $?
0
[Root @ localhost test01] #

P11
[Root @ localhost test01] # Cat File
If [! -E "$1"]
Then
Echo "$1 do not exist"
Fi
[Root @ localhost test01] #./file on
[Root @ localhost test01] # echo $?
0
[Root @ localhost test01] #

P16
[Root @ localhost test01] # Cat greetings
Hour = $ (date | cut-C 19-20)
If ["$ Hour"-GE 0-a "$ Hour"-le 11]
Then
Echo "goodmoring"
Else
If ["$ Hour"-ge 12-a "$ Hour"-le 17]
Then
Echo "Good afternoon"
Else
Echo "Good evening"
Fi
Fi
[Root @ localhost test01] #./greetings
Good afternoon
[Root @ localhost test01] #

[Root @ localhost test01] # Cat G2
Hour = $ (date | cut-C 19-20)
If ["$ Hour"-GE 0-a "$ Hour"-le 11]
Then
Echo "GM"
Elif ["$ Hour"-ge 12-a "$ Hour"-le 17]
Then
Echo "Ga"
Else
Echo "Ge"
Fi

[Root @ localhost test01] # chmod + x G2
[Root @ localhost test01] #./G2
GA
[Root @ localhost test01] #

P18
[Root @ localhost test01] # Cat number
If ["$ #"-ne 1]
Then
Echo "enter a number (Thank you )"
Exit 1
Fi
Case "$1"
In
0) echo zero ;;
1) Echo One ;;
2) Echo Two ;;
3) Echo Three ;;
4) Echo Four ;;
5) Echo Five ;;
6) Echo Six ;;
7) Echo Seven ;;
8) echo eight ;;
9) Echo Nine ;;
Esac
[Root @ localhost test01] #./Number
Enter a number (Thank you)
[Root @ localhost test01] #./Number 8
Eight
[Root @ localhost test01] #

// Version 2
[Root @ localhost test01] # Cat number
If ["$ #"-ne 1]
Then
Echo "enter a number (Thank you )"
Exit 1
Fi
Case "$1"
In
0) echo zero ;;
1) Echo One ;;
2) Echo Two ;;
3) Echo Three ;;
4) Echo Four ;;
5) Echo Five ;;
6) Echo Six ;;
7) Echo Seven ;;
8) echo eight ;;
9) Echo Nine ;;
*) Echo "parameter error brother: please use the digit ";;
Esac
[Root @ localhost test01] #./Number 22
Parameter Error brother: Please use a number
[Root @ localhost test01] #

Write a Applet and obtain a character through the parameter to determine the character type. (Numbers, lower-case letters, upper-case letters, and special characters)
P21.

[Soflib @ localhost ~] $ Cat ctype
If ["$ #"-ne 1]
Then
Echo "Usage: ctype character"
Exit 1
Fi
Char = "$1"
Echo $ char
Numchar = $ (echo-N $ char | WC-C)
Echo "$ numchar"
If ["$ numchar"-ne 1]
Then
Echo "enter a single word"
Exit 1
Fi
Case "$ char"
In
[0-9]) echo "Number ";;
[A-Z]) echo "lowercase letter ";;
[A-Z]) echo "uppercase letters ";;
*) Echo "special characters ";;
Esac
[Soflib @ localhost ~] $ Bash ctype 34
34
2
Enter a single word
[Soflib @ localhost ~] $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.