"Bird's Linux Private cuisine" Chapter 13 Shel Script Exercise Answer

Source: Internet
Author: User

Because the feeling of the Linux command has not much feeling, so specifically to find the bird Brother's book to see, toss a few days to see the basic part of the shell, harvest or pretty big, at least the Linux command is a little feel, and then learn some of the knowledge, in the theoretical knowledge has also been a certain expansion. Not much to say, share the answers to the exercises.

"Bird Brother's Linux private cuisine" Basic article P398 exercises


(1) Please create a new script, when you execute the script, the script can display your current identity (with WhoAmI) and your current directory (with PWD).

The answer to this question has been given directly on the script:

#!/bin/bash# program:# Showing who is and where you are.# history:# 2015/07/26 xpleaf First release Path= $PATH: ~/binexport pathecho "The user is ' $ (whoami) ', and your is in ' $ (PWD) '"

Execution Result:

[[Email protected] test]# sh sh01.sh The user is ' root ', and you're in '/root/scripts/test '

(2) Please write a program yourself, the program can be used to calculate I still have a few days to birthday.

This problem is a bit tangled, the problem is not difficult, but the use of the date command to be familiar with, here I take the practice is:

    1. User input birthdays similar to 19960621 format

    2. Convert the user's birthday to the year's (for example this year is 2015): 20159621

    3. Converts the current date and the user's birthday date (which has been converted to year) to be expressed in seconds

    4. Distance Birthday seconds = User Birthday date seconds-current date seconds

    5. Convert "Distance birthday seconds" to days

    6. Determine the positive or negative of the number of days, so that the user's birthday has not

The script is as follows:

#!/bin/bash# program:#       telling you how many  days are there before your birthday.# history:#        2015/07/26      xpleaf  first releasepath= $PATH: ~/ binexport pathread -p  "please input your birthday like <19960621 > :  " bith_inputbith_input_md=$ (echo  $bith _input | awk  ' begin {fs=" "} {print $5$6$7$8} ') declare -i now_s= ' date +%s ' declare -i bith_s= ' date  --date= "$ (date +%y) $bith _input_md"  +%s ' declare -i total_s=$ (($bith _s-$now _s)) declare  -i total_day=$ (($total _s/60/60/24)) if [  "$total _day"  -gt  "0"  ];then         echo  "there are  $total _day day (s)  before  your bithday. " Elif [  "$total _day"  -eq  "0"  ];then        echo   "Today is your birthday!!! ~happy birthday to you! " else        echo  "Your birthday in this year  had gone! " Fi

Execution Result:

[Email protected] test]# sh sh02.sh Please input your birthday like <19960621>: 20150730There is 3 day (s) before Your bithday.

I feel that this method is more tangled, so I think there is a very simple way, know the trouble to tell me, thank you very much!

(3) Let the user enter a number, the program can be used by the 1+2+3 ... until the number entered by the user.

The same as the topic, but also interactive, but this problem is much simpler, not so much data processing, and in C language such a problem has seen a lot, so is a relatively simple problem, directly on the script:

#!/bin/bash# program:# You input the number, and I caculate 1+2+3+...+the number.# history:# 2015/07/26path= $P Ath:~/binexport Pathecho "You input the number, and I caculate 1+2+3+...+the number." Read-p "Please input the number:" Nufor ((i=s=0;i<= $nu; i++)) do s=$ (($s + $i) doneecho "1+2+3+...+ $nu ==> $s"

Execution Result:

[Email protected] test]# sh sh03.sh You input the number, and I caculate 1+2+3+...+the number. Please input the number:1001+2+3+...+100 ==> 5050

(4) Write a program, its role is to look at the/root/test/logical this name exists, if not, create a file, use touch to create, leave after the creation is complete, if present, determine whether the name is a file, If the file is deleted after the new directory, the file name is logical, then left, if there is, and the name is a directory, then delete this directory!

The topic is relatively long, but the logic is very simple, simply comb the following (assuming that the/root/test directory is already present):

    1. Determine if the name of logical is present in the/root/test directory

      (1) There is no ==> use touch to create logical this file

      (2) Presence

      A.logical is file ==> Delete logical file while creating logical directory

      B.logical is directory ==> Delete logical directory

Obviously, with "&&" and "| |" can be resolved, but below I do not write for the expression so long, for the existence of the judgment I used the IF statement:

#!/bin/bash# program:# Check the/root/test/logical# history:# 2015/07/26 xpleaf First releasepath= $PATH : ~/binexport pathecho "Check the/root/test/logical" Test-e/root/test/logicali=$?if ["$i"-eq "0"];then test-f /root/test/logical && rm-rf/root/test/logical && mkdir/root/test/logical | | Rm-rf/root/test/logicalelse Touch/root/test/logicalfi

Execution Result:

Situation one:/root/test/logical does not exist

[[email protected] test]# ls-l/root/test/logicalls: Unable to access/root/test/logical: No file or directory

Execute the script:

[[Email protected] test]# sh sh04.sh check the/root/test/logical[[email protected] test]# ls-l/root/test/logical-rw-r-- r--1 root root 0 July 11:05/root/test/logical

You can see that the logical file was created.


Scenario Two:/root/test/logical exists and is a file (that is, the result of a previous step)

[[Email protected] test]# sh sh04.sh check the/root/test/logical[[email protected] test]# ls-ld/root/test/ Logicaldrwxr-xr-x 2 root root 4096 July 11:06/root/test/logical

As you can see, the logical has become a directory, that is, the logical file is deleted, and the logical directory is created at the same time.


Scenario Three:/root/test/logical exists and is a directory (that is, the result of the previous step)

[[Email protected] test]# sh sh04.sh check the/root/test/logical[[email protected] test]# ls-l/root/test/logicalls: Unable to visit Q/root/test/logical: There is no file or directory

As you can see, there is no/root/test/logical at this time, that is, the logical directory created in the previous step has been deleted.

(4) We know/etc/passwd in: to separate, first listed as the name of the account. Please write a program that can take out the first column of/etc/passwd, and each column is displayed in a line of string "The 1 account is" root ", the 1 represents the number of rows.

You can do this directly with the powerful awk:

#!/bin/bash# program:# Remove The first list of the passwd and then output it# like "The Count line one Is:t  He content of the passwd.# history:# 2015/07/26 xpleaf First releasepath= $PATH: ~/bainexport pathecho "$ (awk-f ":" ' {print "the" NR "account is" $ '/etc/passwd) "

Execution Result:

[[Email protected] test]# sh sh05.sh the 1 account is rootthe  2 account is binThe 3 account is daemonThe 4 account  is admthe 5 account is lpthe 6 account is syncthe 7  account is shutdownthe 8 account is haltthe 9 account is  mailthe 10 account is uucpthe 11 account is operatorthe 12  account is gamesthe 13 account is gopherthe 14 account is  ftpthe 15 account is nobodythe 16 account is dbusthe 17  account is vcsathe 18 account is abrtthe 19 account is  Haldaemonthe 20 account is ntpthe 21 account is saslauththe 22  account is postfixthE 23 account is sshdthe 24 account is tcpdumpthe 25 account  is oldboythe 26 account is testthe 27 account is apachethe  28 account is wwwthe 29 account is mysql

This problem is a bit of a pit, if you know that the problem is the test of awk or itself is very familiar with awk, can be desired at a moment, but if the previous two conditions are not, then the trouble! At first I did not expect to use awk, using a very bitter method: to travel number, you have to use the loop, starting from 1 to add, to use the loop, you need to know the condition of the loop end, that is to know the/etc/passwd of the number of rows, to know the number of rows, you have to use WC Then use the cut to fetch the first column, and then use SED to output each line (also with loops).

Unfortunately at the end of the blind toss, theoretically it can be achieved, I have done on the Linux command line, but in the use of vim editing, the following line is problematic, that is, the syntax of the SED:

user=$ (sed-n ' $ip '/etc/passwd | cut-d ': '-F 1)

' $ip ' here, actually I want to control the output of the first few lines, I is a loop variable, but here is not recognized, so finally do not, fortunately there is a strong awk, otherwise it will pit Ah! Two ways, that's too far away AH!

On this grammar above, I also want to know why not Ah, know the trouble to tell a cry, thank you very much Ah!

This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1678565

"Bird's Linux Private cuisine" Chapter 13 Shel Script Exercise Answer

Related Article

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.