Summary of variable usage in Shell
This article mainly introduces the usage of variables in Shell. This article summarizes the syntax and common usage of variables, and provides sample code. For more information, see
Variable definition. It can only start with letters and underscores (_). It is case-sensitive and can contain numbers, letters, and underscores (_). For details, see the official manual.
The Code is as follows:
[Root @ svn shell_example] # yourname = 'linux'
[Root @ svn shell_example] # echo $ yourname
Linux
[Root @ svn shell_example] # YourName = "linux"
[Root @ svn shell_example] # echo "The variable is $ YourName"
The variable is linux
Obtain the script of the current date, which is often used in log cutting. To distinguish daily log files
The following code is generated on January 1, April 27, 2015.
The Code is as follows:
[Root @ svn shell_example] # echo 'date + % Y % m % d'
20150427
[Root @ svn shell_example] # cat today. sh
#! /Bin/bash
TODAY = 'date + %'
YEAR = 'date + % Y % m month % dday'
Echo "TODAY is $ YEAR, $ TODAY"
[Root @ svn shell_example] # sh today. sh
Today is Monday, January 1, April 27, 2015.
Obtain user input from the keyboard. After the following script is executed, the script waits for user input directly until user input is complete and press Enter. The script obtains user input and prints the result.
The Code is as follows:
[Root @ svn shell_example] # cat var. sh
#! /Bin/bash
Read myvar
Echo "myvar is $ myvar
The execution result is as follows:
[Root @ svn shell_example] # sh var. sh
Linux
Myvar is Linux
[/Code]
Get user input from keyboard with prompt information
The Code is as follows:
#! /Bin/bash
# Obtain the user information from the keyboard and print it out.
Echo-n 'enter Your Name: '#-n indicates no line feed.
Read name
Echo "Hi $ name"
Sh name. sh
Enter Your Name: linux
Hi linux
Upgrade script, which successfully accepts the user's two parameter surnames and names, and prints the input on the screen
The Code is as follows:
The Code is as follows:
[Root @ svn shell_example] # cat firstname. sh
#! /Bin/bash
Pw = "123"
Echo
Echo "+ ========= User Logon =========+"
Echo
Echo-n "Enter your user name :"
Read fname lname
Echo-n "enter your password :"
Read passwd
Echo "Dear member $ fname $ lname, hello! You have logged on successfully"
[Root @ svn shell_example] # sh firstname. sh
+ ========= User Logon ===========+
Enter your Username: nasli
Enter your password 123
Dear member, thank you! You have logged on
Note <>: For more exciting tutorials, please pay attention to the help house programming