Shell script exercises and Instances

Source: Internet
Author: User

Shell scripts are used to record some exercise questions and shell scripts I have actually written here for learning and querying.

  • User input y or y ends

While Mode
#! /Bin/bash
Read-P "input you choice:" C
While ["$ C "! = "Y"-a "$ C "! = "Y"]
Do
Read-P "input you choice:" C
Done
Echo "OK! "

Until Mode
#! /Bin/bash
Until ["$ C" = "Y"-o "$ C" = "Y"]
Do
Read-P "input your choice:" C
Done
Echo "done! "

  • How to Use Case, function, and function parameters

#! /Bin/bash
Function print ()
{
Echo "your choice is $1"
}

Case $1 in
"One ")
# Echo "one"
Print 1
;;
"Two ")
# Echo "two"
Print 2
;;
"Three ")
# Echo "othre"
Print 3
;;
*)
Echo "usage $0 {one | two | three }"
;;
Esac

  • How to execute commands in shell scripts

#! /Bin/bash
STR = $ (whoami) # Or: Str = 'whoamam' (the quotation marks here are backticks -- the key under ESC)
Echo $ Str

  • How to use for in shell scripts

Read every substring in the string cyclically (separated by spaces)
#! /Bin/bash
String = "waiting for a while"
For STR in $ {string}
Do
Echo $ Str
Done

Read every substring in the string cyclically (separated by spaces)
#! /Bin/bash
For STR in "waiting for a while"
Do
Echo $ Str
Done

Read every result returned by the LS command cyclically
#! /Bin/bash
For STR in 'LS'
Do
Echo $ Str
Done

For () is supported by bash.
#! /Bin/bash
For (I = 0; I <10; I ++) # Or: for I in {1 .. 10}
Do
Echo $ I
Done

  • Dirname command and script statement $ (dirname "$ {0 }")

Display the path prefix of the specified path except the last file name or directory name,
That is, the contents of the specified path are truncated. For example, run dirname/usr/bin/sort to get/usr/bin.

This statement is often seen in shell scripts: $ (dirname "$ {0 }")
Note: $ {0} indicates the first parameter of the current script, that is, the complete path of the program. Then, run the dirname command,
The directory where the current script is located is obtained.

Statement: CD $ (dirname "$0") | Exit 1
Note: $ (dirname "$0") indicates the directory where the current script is located, and CD indicates the directory where the script is located,
If it fails, Exit 1 (exit ).

The more common and accurate syntax is:

This_dir = $ (readlink-F "$ (dirname" ${0 }")")

  • Grep

The following statement is displayed in the shell script today:

Num_jobs = "$ (grep-c" ^ processor "/proc/cpuinfo )"

The file/proc/cpuinfo is the CPU information. grep-C only counts the number of rows in the query results but does not output them. The regular expression "^ processor" indicates the row starting with processor, view the/proc/cpuinfo file and you can see that the line starting with processor is the core information of the CPU. In summary, the preceding shell statement counts the number of CPU cores of the current machine.

  • Determines whether the string is a number.

function isDigit(){if [ $# == 1 ];thenif [ `echo $1 | grep -P  "^\d+$" | wc -l` == 1 ];thenreturn 0;fifireturn 1;}isDigit '111'echo $?isDigit 'ass'echo $?

  • Grep

The following statement is displayed in the shell script today:

Num_jobs = "$ (grep-c" ^ processor "/proc/cpuinfo )"

The file/proc/cpuinfo is the CPU information. grep-C only counts the number of rows in the query results but does not output them. The regular expression "^ processor" indicates the row starting with processor, view the/proc/cpuinfo file and you can see that the line starting with processor is the core information of the CPU. In summary, the preceding shell statement counts the number of CPU cores of the current machine.

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.