Linux Shell programming from beginner to proficient in exercises answers after class answers (Chapter 8)

Source: Internet
Author: User

This book has no answer. If you send an email to the author, he will not provide the answer. So I had to answer the question myself.

Chapter 5

5.1

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Q Cai-English-1984-Jiangsu-7

Cat passwd | sort-r-n-t ":"-K3 | awk-F ":" '{print $1 "\ t" $3, $4, $5 }'


Chapter 8

8.1 1 ~ Add an even number in 100 using for, while, and.

#! /Bin/bash # Use forsum1 = 0for (I = 1; I <= 100; I ++) do if [$ ($ I % 2 )) -EQ 0] Then sum1 =1 ($ sum1 + $ I )) fidoneecho "sum1 =" $ sum1 # Use whilesum2 = 0j = 0 while [$ J-Le 100] Do sum2 =$ ($ sum2 + $ J )) j = $ ($ J + 2 )) doneecho "sum2 =" $ sum2 # Use untilsum3 = 0 k = 0 until [$ K-GT 100] Do sum3 =3 ($ sum3 + $ k )) k = $ ($ K + 2) doneecho "sum3 =" $ sum3 8.2 use while to read the string from the command line, so that the input string ends with a full stop.#! /Bin/bashread-P "Please input:" argswhile ["$ ARGs "! = "."] Do read-P "not right, please input:" argsdoneecho "Right! "8.3 different
8.4 use for to output 2, 4, 8, 16, 32, 64. Use while and until to achieve the same #! /Bin/Bashi = 2 printf "% d \ t" $ IFOR (j = 1; j <= 5; j ++ )) do I = $ ($ I + $ I) printf "% d \ t" $ idoneprintf "\ n" 8.5 loops are superimposed from 1, until the sum is greater than 2000 #! /Bin/Bashi = 1sum = 0 while ["A" = "A"] Do sum = $ ($ sum + $ I )) echo $ sum if [$ sum-ge 2000] The number of then break fidone8.6 values less than 100 divided by 3. Each line shows 8. #! /Bin/bashj = 1for (I = 1; I <= 100; I ++) do if [$ ($ I % 3 )) -EQ 0] Then printf "% d" $ I if [$ J-lt 8] Then printf "\ t" let "J ++" else printf "\ n" J = 1 fi fidoneprintf "\ n"

8.7 print * the image.

*

***

*****

*******

*********

#! /Bin/bash

I = 0

K = 1

For (j = 1; j <= 6; j ++ ))

Do

While [$ K-lt $ I]

Do

Echo-n "*"

K = $ ($ k + 1 ))

Done

Printf "\ n"

I = $ ($ I + 2 ))

K = 1

Done

8.8 enter an integer, calculate the sum of the numbers, and determine whether the number can be divisible by 9. If this number is equal to or greater than 9, the result is output.#! /Bin/bashread-P "input a number: "numsum = 0j = $ numwhile [$ num-GT 0] Do J = $ num % 10 num =$ ($ num/10 )) sum = $ ($ sum + $ J) doneecho "sum =" $ sumif [$ ($ sum % 9)-EQ 0] Then ECHO "yes, the number can be divisible by 9 "else echo" No, the number cannot be divisible by 9 "fi

8.9 enter an integer to calculate the sum of odd numbers and even numbers.


8.10 judge whether an integer is a prime number. Prime Number (an even number only has 2 as a prime number, and an odd number cannot be divisible by any odd number less than or equal to its square root)

#! /Bin/bash


Read-P "input:" In

If [$ ($ in % 2)-EQ 0]

Then

If [$ in-EQ 2]

Then

Echo "yes"

Else

Echo "no"

Fi

Else

For (I = 1; I <$ in; I ++ ))

Do

If [$ ($ in % $ I)-EQ 0]

Then

Echo "no"

Exit 0

Fi

Done

Fi

Echo "yes"

This method is not optimized.
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.