Bash Programming: Shell Exercises

Source: Internet
Author: User
Tags greatest common divisor

Bash Programming: Exercises

1. Write a script: If a path does not exist, it is created as a directory, otherwise it is present, and the content type is displayed

#!/bin/bash#baseurl=/var/tmp/testdirif [-e $baseurl]; Thenecho "file is no exists." Elsemkdir-p $baseurlfile $baseurlfi

2. Write a script to complete the following functions; Determine the given two values, whichever is smaller; method of a given number: script parameter, command interaction

#!/bin/bash#if [$#-lt 1]; Thenecho "Plz Enter and digit" Exit 1fiif [$1-GT]; Thenecho "max;$1 min:$2" Elseecho "max:$2 min:$1" fi

3. Ask for the sum of all odd numbers within 100 (at least 3 methods)

(1) #!/bin/bash#declare-i sum=0for i in $ (SEQ 1 2 100); dosum+= $idoneecho "sum: $sum." (2) #!/bin/bash#declare-i sum=0for i in {1..100}; doif [$[$i%2]-eq 1]; thensum=$ (($sum + $i)) Fidoneecho "sum: $sum" (3) #!/bin/bash#declare-i sum=0he () {For I in $ (SEQ 1 2); dosum=$ (($sum + $i )) Done}heecho "sum: $sum."

4. Write a script to implement the following functions:

(1) Pass two text file path to script;

(2) Show the number of blank lines in two files and their blank lines;

(3) Show the total number of files in two files and their total number of rows;

#!/bin/bash# #if [$#-lt 1]; then# echo "Plz enter" and "Opt." # Exit 1#fispace1=$ (grep "^$"/root/file |wc-l) space2=$ (grep "^$"/root/file1 |wc-l) line1=$ (cat/root/file |wc-l) line2= $ (Cat/root/file1 |wc-l) if [$space 1-gt $space 2]; Thenecho "Kongduode is/root/file: $space 1 Lines" Elseecho "Kongduode is/root/file1: $space 2 Lines" Fiif [$line 1-gt $line 2]; Thenecho "Zonghangduo is/root/file: $line 1 Lines" Elseecho "Zonghangduo is/root/file1: $line 2 Lines" fi

5. Write a script

(1) Prompt the user to enter a string;

(2) Judgment:

If the input is quit, exit the script;

Otherwise, the string content of its input is displayed;

#!/bin/bash#while true; Doread-p "Plz Enter character:"-T ten char[$char = = "Quit"]&& Breakecho "character is A: $char" Done

6. Write a script to print the 2^n table; n equals a user-entered value

#!/bin/bash#read-p "Plz Enter a num:"-T ten numdeclare-i i=0while [$i-le $num];d olet i++echo-n-E "2^ $i =$[2** $i]" EC Hodone

7. Write a script, write a few functions: function 1, achieve the sum of two values given, function 2, take the greatest common divisor of the given two values, function 3, take the least common multiple of the given two numeric values, and the size of the selected and two numeric values of the function will be provided through interactive input. Note: Really do not know the number of conventions, common multiple, copy of the Chino students homework

#!/bin/bash#if [[$1-eq] "]];thenecho ' plz input the first number ' exit 1fiif [[$2-eq] ']];thenecho ' plz input the SEC  Ond number ' exit 2fifunction Add () {Sum=$[$1+$2]echo "The sum is $sum"}add $2declare-i maxdeclare-i minif [[$1-gt $ ]];thenmax=$1min=$2elif [[$1-lt $]];thenmax=$2min=$1elsemax=$1min=$2fifunction gong () {r=$[$max% $min]temp=$ Mindeclare-i adeclare-i bif [[$r-eq 0]];thenecho "Gongyue is $min" echo "Gongbei is $max" Elsewhile [[$r-ne 0]];d oa= $2b= $rr =$[$a% $b]donegongyue= $bgongbei =$[$1*$2/$b]echo "Gongyue is $gongyue" echo "Gongbei is $gongbei" Fi}gong $


Bash programming: Shell exercises

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.