The operation of a numeric variable:
The numerical operation of the shell variable is mostly used in the process control of the script, in the shell environment, only a simple integer operation, does not support the fractional operation, the integer value operation mainly through the internal command expr execution. The following is the basic format
Expr variable 1 operator variable 2 [operator variable 3] ...
Where, variable 1, variable 2 .... Corresponds to a numeric variable that needs to be computed (called with a "$" symbol), as follows are several commonly used operators:
+: addition operation
-: Subtraction operation
\*: Multiplication, note that you cannot use only the "*" symbol, otherwise you will be treated as a file wildcard
/: Division operation
%: Take the remainder operation
Cases:
[Email protected] ~]# x=35
[Email protected] ~]# y=16
[[email protected] ~]# expr $X + $Y//+ There are spaces on both sides.
51
[Email protected] ~]# expr $X-$Y
19
[Email protected] ~]# expr $X \* $Y
560
[Email protected] ~]# expr $X/$Y
2
[Email protected] ~]# expr $X% $Y
3
Example: Computes the three-pass of the variable y and assigns the result to the variable ycube.
[Email protected] ~]# ycube= ' expr $Y \* $Y \* $Y '
[Email protected] ~]# echo $Ycube
4096
Second, special shell variables
Environment variables: Used to set the user's working environment, including user host directory, command lookup path, user's current directory, login terminal, etc. The value of the environment variable is automatically maintained by the Linux system and will change as the user state changes. Example: Use the ENV command to view environment variables in the current environment.
Mail=/var/spool/mail/root
Path=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Pwd=/root
Lang=en_us. UTF-8
Ssh_askpass=/usr/libexec/openssh/gnome-ssh-askpass
Histcontrol=ignoredups
Shlvl=1
Home=/root
Logname=root
The 2.PATH variable is used to set the default search path for executable programs, and when you specify only the file name to execute the command program, Linux will look for the corresponding executable file in the directory range specified by the path variable and will prompt "command not found" if it is not found, for example:
[Email protected] ~]# ls-lh/script/
Total 4.0K
-rwxr-xr-x 1 root root June 21:53 first.sh
[Email protected] ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[Email protected] script]# first.sh
-bash:first.sh:command not found
[[email protected] ~]# path= "$PATH:/script"
[Email protected] ~]# echo $PATH
/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root:/script
[Email protected] ~]# first.sh
In the current directory:
/boot
Begin with VML files including:
-rwxr-xr-x. 1 root root 4.0M vmlinuz-2.6.32-431.el6.x86_64
3. In a Linux system, the global configuration file for environment variables is/etc/profile, and the variables defined in this file act on all users, in addition to each user's own separate configuration file (~/.bash_profile). To change or set an environment variable for a long time, you should set it in the above file, for example: Change the History command bar number of records to 200 (the default is 1000), only for root users.
[Email protected] ~]# Cat/root/.bash_profile
#. Bash_profile
# Get the aliases and functions
If [-f ~/.BASHRC]; Then
. ~/.bashrc
Fi
# User specific environment and startup programs
Path= $PATH: $HOME/bin
Export PATH
Exprot histsize=200
[Email protected] ~]# history |wc-l
387
[Email protected] ~]# Source/root/.bash_profile
[Email protected] ~]# history | Wc-l
200
4. Positional variables: Positional variables are also known as positional parameters, using $1,$2,$3,...$9 representations, for example. When the command line "Ls-lh/boot" is executed, where the first variable is "-LH", the second position variable is "/boot", expressed in $. The name of the command or script itself is denoted by "$". Cases:
[email protected] script]# cat sum.sh
#!/bin/bash
sum= ' expr + $ '
echo "$ + $ = $SUM"
[Email protected] script]# chmod +x sum.sh
[Email protected] script]#./sum.sh 38 47
38 + 47 = 85
5. Predefined variables: There is a pre-defined bash program a class of special variables, users can only use, can not be created, and then I will enumerate:
$#: Represents the number of positional parameters in the command line.
$*: Represents the contents of all positional parameters
$?: Represents the return status of the current command after execution, the return value of 0 means that the execution is correct, and returns any non-0 number representing the exception, followed by explanation.
$ A: Represents the currently executing script or program name.
Example: In order to illustrate the role of predefined variables, a backup script is written below, which is used to package multiple files or directories specified by the command line, and to output relevant information, in which the new compressed package file name is embedded in seconds, and the "date +%s" command gets the time of the second tick.
[email protected] script]# cat mybak.sh
#!/bin/bash
tarfile=beifen-' Date +%s '. tgz
Tar zcf $TARFILE $* &>/dev/null
echo "Executed $ script"//executed A
echo "Executed $# A Backup objects"//total $ #个对象备份
echo "A Backup objects: $*"//specifically included
[Email protected] script]# chmod +x mybak.sh
[Email protected] script]#/mybak.sh/boot/grub//etc/
Executed./mybak.sh Script
Executed 2 A Backup objects
A Backup objects:/etc/
[[email protected] script]# LS-LH beifen-*//Confirm backup Script
-rw-r--r--1 root root 9.7M June 22:08 beifen-1466690927.tgz
OK today say so much, slowly, to understand, tomorrow happens to have time to write if statement.
This article is from the "one dish can no longer dish of the Little rookie" blog, please be sure to keep this source http://liliming.blog.51cto.com/10925034/1792372
Shell Script basic Application (iii) Special shell variables