This article mainly introduces the variables in the shell use summary, this article summed up the variable syntax, common use forms and so on, and give the code examples, the need for friends can refer to the
The definition of a variable can only begin with a letter and an underscore, case-sensitive, and may contain a numeric letter underline. See Official Handbook
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
Gets the script for the current date, commonly used in log cutting, in order to distinguish daily log files
Like the following generation 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 +%a '
Year= ' date +%y year%m month%d days '
echo "Today is $YEAR, $TODAY"
[ROOT@SVN shell_example]# SH today.sh
Today is April 27, 2015, Monday
From the keyboard to obtain user input, the following script executes the script directly wait for the transfer input, until the user input completes and returns, the script obtains the user's 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
Implementation results are 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
#从键盘获取用户信息, and print it out.
Echo-n ' Enter Your Name: ' #-n option indicates no line wrapping.
Read name
echo "Hi $name"
SH name.sh
Enter Your Name:linux
Hi Linux
Upgrade script, which successfully accepts the user's 2-parameter surname and name, and prints the input on the screen
The code reads as follows
The code is as follows:
[Root@svn shell_example]# Cat firstname.sh
#!/bin/bash
pw= "123"
Echo
echo "+======== User Login =========+"
Echo
Echo-n "Please enter your username:"
Read FName lname
Echo-n "Please enter your password:"
Read passwd
echo "Dear member $fname $lname, Hello!" You are logged in successfully "
[ROOT@SVN shell_example]# SH firstname.sh
+======== User Login =========+
Please enter your username: Nass Lee
Please enter your password: 123
Dear member Nass Li, Hello! You have logged in successfully
Note < > : More Wonderful tutorials please focus on Triple programming