September 13 Course Assignments (practice)

Source: Internet
Author: User
Tags greatest common divisor

First, homework (exercise) content:

1. Describe the operation principle of shell program (can be attached with necessary graphic instructions);

2, summarize all the knowledge points involved in shell programming (such as: variables, grammar, command status, etc., to take the picture of Yo);

3, summarize the course of all the circular statement, the use of conditional judgment and related examples; (if (jpg|png is not exist); echo "You say a XX")

4. Summarize the usage of the Text Processing tool sed and awk (must accompany the example)

5. Write a script: If a path does not exists, it is created as a directory, otherwise it is present and the content type is displayed; (Don't doubt, it's that simple)

#!/bin/bash# determine if a path exists, if present, display its content type, otherwise create a directory with the same name # method of the given path: script parameter  if  [ $# -lt 1 ];then        echo  " This script is used to determine if a path exists, please enter at least one path after the script "        exit 1 fi for  i in $*;d o    if [ -e  $i  ];then         echo   "$i already exists"         file   $i     else        mkdir -p  $i         echo  "Successfully created directory $i"          echo -e  "$ (dirname  $i) directory with List of all files and subdirectories:  \n$ (ls  ' dirname  $i ')"       fi done 

6, write a script to complete the following functions:

Judge a given two values, whichever is smaller;

Method of given number: script parameter, command interaction; (using read, still so simple)

methods for determining values: Script Parameters

#!/bin/bash# to determine the given number of two numeric size # methods: script parameters [ $# -ne 2 ]  && echo  "This script is used to determine the size of a given two numeric values, please enter two integers after the script"  && exit 0num1=$ (Echo  $1 | grep  ' ^[0-9]\+$ ') num2=$ (echo $2 | grep  ' ^[0-9]\+$ ') [ -z   $num 1 ] && [ -z  $num 2 ] && echo  "$1,$2 not an integer"  && exit 0[ -z  $num 1 ] && echo  "$ not integer"   && exit 0[ -z  $num 2 ] && echo  "$ Not an integer"  &&  exit 0[ $1 -eq $2 ] && echo  "input two number Same"  &&  exit 0[ $1 -gt $2 ] && echo  "$ greater than $"  &&  exit 0[ $1 -lt $2 ] && echo  "$ less than $"  &&  exit 0 

#!bin/bash# determines the size of a given two numeric value # method of given number: command interaction echo  "This script is used to determine the given two numeric size" read -p  "Please enter two integers in 10 seconds: "  - t 10 i j[ -z  $i  ] && [ -z  $j  ] &&  echo  "Please re-enter two integers"  && exit 0[ -z  $j  ] && echo   "The second number is empty, please enter two integers"  && exit 0num1=$ (echo  $i  | grep  ' ^[0-9]\+$ ') num2=$ (echo  $j  | grep  ' ^[0-9]\+$ ') [ -z  $num 1 ] && [  -z  $num 2 ] && echo  "$i, $j not an integer"  && exit 0[ -z   $num 1 ] && echo  "$i is not an integer"  && exit 0[ -z $ num2 ] && echo  "$j not integer"  && exit 0[  $i  -eq  $j  ] && echo  "input two numbers same"  && exit 0[  $i  -gt  $j  ] && echo  "$i greater than $j"  && exit 0[  $i  -lt  $j  ] &&  echo  "$i less than $j"  && exit 0

#!bin/bash# 100 for all odd and echo  "one means using the first method to find all odd and" echo  "two means using the second method to obtain the sum of all odd numbers within 100 and" echo  " Three means using the third method to obtain all the odd and "read -p " within 100, enter any one of the words in the one|two|three in 10 seconds:  " -t 10 val[  -z  $val  ] && echo  "Please enter any word in One|two|three"  && exit  0declare -i sum=0declare -i j=1case  $val  inone) for i in $ ( seq 1 2 100);d o    sum=$[$sum + $i]doneecho  "The sum of all the odd numbers within 100 is calculated using the For Loop Step 2 method : $sum "exit 0;; for  ((i=1;i<=100;i++));  do   [ $[$i%2] -eq 1 ] & & sum=$[$sum + $i]doneecho  "uses a For loop to take 2 modulo 1 to find out that all the odd sum within 100 is: $sum" exit 0;; Three) while [  $j  -le 100 ];d o    sum=$[$sum + $j]    let j+=2doneecho  "The sum of all the odd numbers within 100 is calculated with a while loop step of 2: $sum" exit 0;; *)  echo  "input error, please select any option in One|two|three" ExiT 0esac 

8, write a script to achieve 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# method for judging the number of lines in a text file echo  "This script is used to determine the number of lines of two text files given" read -p  "Please enter two text file paths in 20 seconds:"  -t 20  file1 file2[ -z  $file 1 ] && [ -z  $file 2 ]  && echo  "Please enter two text file path"  && exit 0[ -z  $file 2 ]  && echo  "The second file path is empty, please enter two text file path"  && exit 0[ ! -f  $file 1  ] && [ ! -f  $file 2 ] && echo  " The two file path entered does not exist or is not a text file, please re-enter " && exit 0[ ! -f  $file 1 ] & & echo  "The first file path entered does not exist or is not a text file, please re-enter"  && exit 0[ ! -f $ file2 ] && echo  "The second file path entered does not exist or is not a text file, please re-enter"  && exit 0i=$ ( grep  ' ^$ '   $file 1 | wc -l) j=$ (grep  ' ^$ '   $file 2 | wc -l) [   $i  -eq  $j  ] && echo -e  "$file 1 files and $file2 files have the same number of blank lines  \n  total blank lines: $i line"   [  $i  -gt $ j ] && echo -e  "A file with a large number of blank rows is a blank line in $file1 \n : $i line"  [  $i  - lt  $j  ] && echo -e  "A file with more blank lines is $file2 \n  blank line: $j line"  k=$ (WC  -l  $file 1 | cut -d '   '  -f1) l=$ (wc -l  $file 2 | cut  -d '   '  -f1) [  $k  -eq  $l  ] && echo -e  "$file 1 files and The total number of rows in the File2 file is the same  \n  total: $k line   [  $k  -gt  $l  ] && echo  -e  "More rows of files are $file1 \n  total: $k line"  [  $k  -lt  $l  ] &&  echo -e  "More rows of files are $file2 \n  total: $l line"


9. 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# sentenced the user to enter a string read-p "Please enter a string in 15 seconds:"-t str[-Z $STR] && echo "Please enter a string" && exit 0[[$str = = ' Quit '] && echo "exited" && Exit 0 | | echo "Input string: $str"

10, write a script, print 2^n table; n equals the value entered by a user; (Sorry, I'm naughty)

#!/bin/bash# print 2^n table; n equals a user-entered value k=1read-p "Please enter an integer in 5 seconds:"-T 5 var[-Z $var] && echo "Please enter an integer" && exit 0num            =$ (echo $var | grep "^[0-9]\+$") [-Z $num] && echo "Input error, please enter an integer" && exit 0for ((i=0;i<= $var; i++));d o echo "2^${i}= $k" K=$[${k}*2]done

11, write a script, write a few functions:

Function 1, to achieve the sum of the given two values;

Function 2, take a given two values of greatest common divisor;

Function 3, take a given two values of least common multiple;

              about the selection of functions, the size of the two numeric values will be provided via interactive input

The #!/bin /bash# function 1 implements the sum of the given two values, #函数2, takes the greatest common divisor of the given two values, #函数3, takes the least common multiple # of the given two values, and the size of the selected and two numeric values of the function is provided through interactive input read  -p  "Please enter two integers in 7 seconds:"  -t 7 i j[ -z  $i  ] && [ -z   $J  ] && echo  Please reenter two integers  && exit 0[ -z  $j  ] && echo  "The second number is empty, please enter two integers"  && exit 0num1=$ (echo  $i  | grep  ' ^[0-9]\+$ ') num2=$ (echo  $j  | grep  ' ^[0-9]\+$ ') [ -z  $num 1  ] && [ -z  $num 2 ] && echo  "input error, $i, $j not an integer"   && exit 0[ -z  $num 1 ] && echo  "input error, $i not an integer"  & & exit 0[ -z  $num 2 ] && echo  "input error, $j not an integer"  &&  exit 0 sum  ()  {           sum =$[${i}+${j}]            echo  "Sum of two inputs: $Sum"      } max  ()  {      if [  $i  -eq $2 ]; then             echo  "The input of the two number of greatest common divisor is: $i"              exit 0      elif [  $i  -gt  $j  ];then       for  num1 in $ (seq  $j  -1 1);d o                [ $[$i% $num 1] -eq 0 ] && [ $[$j % $num 1] -eq 0 ] && echo  "the greatest common divisor of the two numbers entered is: $num 1"  && exit  0      done     else        for  num2 in $ (seq  $i  -1 1);d o           [ $[$i% $num 2] -eq 0 ] && [ $[$j% $num 2] -eq 0 ]  && echo  "Two greatest common divisor of input: $num 2"  && exit 0         done      fi      }  min  ()  {     if [  $i  -eq  $j  ];then            echo  "Two least common multiple of input: $i"              exit 0     elif [  $i  -gt  $j  ];then         for num1 in  $ (seq  $i  1 $[$i * $j]);d o             [ $[$num 1% $i] -eq 0 ] && [ $[$num 1% $j] -eq 0 ] && echo  "The least common multiple of the two numbers entered is; $num 1"  && exit 0          Done     else         for num2  in $ (seq  $j  1 $[$i * $j]);d o             [ $[$num 2% $i] -eq 0 ] && [ $[$num 2% $j] -eq  0 ] && echo  "Two least common multiple of input, $num 2"  && exit 0          done     fi    } echoecho  " sum represents the sum of the two digits of the input and" echo  " max represents the two number of inputs for the greatest common divisor" echo  " min represents the two number of inputs for the least common multiple" read -p  "Please enter any one of the words in the sum|max|min in 10 seconds:"  -t 10 str[ -z  $str  ] & & echo  "Please enter SUM|MAAny one word in x|min " && exit 0case  $str  insum)     sum      ;; Max)    max    ;; MIN)    min    ;; *)   echo  "input error, please enter any word in sum|max|min"   exit 0esac


September 13 Course Assignments (practice)

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.