Shell script is similar to batch processing under Windows/dos, that is, the use of various commands pre-placed into a file, convenient one-time execution of a program file, mainly for the convenience of administrator settings or management. The advantage of scripting languages over Perl and Python is that it handles the underlying business because there are a lot of Linux commands to support, such as "Alarm Service", "one-click Installation" and so on, which is very simple to write with shell scripts.
The shell script under Linux defaults to bash, and SH is actually a link to bash.
Two ways to view the default shell:
>echo $SHELL
/bin/bash
>grep root/etc/passwd
Root:x:0:0:root:/root:/bin/bash
a simple script:
Previously in this article to configure the static IP bridge is manual operation, the next time a network and reconfigure, so wrote a script automatically configured. Learn a simple shell script based on this script.
#!bin/bash
etc_dir=/etc/sysconfig/network-scripts
net_dir=/etc/sysconfig
root_uid=0
If ["$UID"- Ne "$ROOT _uid"] #判断是否为root权限
then
echo "must is ROOT to run this script"
exit 1
fi
cd $ETC _dir | | {
echo "cannot change to necessary directory," >&2
exit 1
}
cat/dev/null > Ifcfg-eth0 && echo "clean ifcfg-eth0 ok!"
#写入eth配置:
cat/home/yangni/staticip/myip >ifcfg-eth0 && echo "Write eth0 sucessfully"
cd $NET _dir || {
echo "cannot change to necessary directory," >&2
exit 1
}
cat/dev/null >network< c23/> #清空network
#将默认网关相关配置写入network
cat/home/yangni/staticip/mynetwork > Network && echo " Write Network sucessfully "
service network restart #重启网络命令
exit 0
1, UID is a system variable, is currently the root permission uid=0, the current non-root permission UID is not equal to 0
So we set a variable root_uid=0 here to determine whether the current root state, because we have to modify the system configuration file must use root permissions. Therefore, you must add sudo when executing. 2. Three ways to clear files:
Three ways to empty the log:
1, echo "" >test.log
2, Echo >test.log
3, Cat/dev/null >test.log
In the code we use the third method: Cat/dev/null >network 3, && | | Use of:
Cat/dev/null >ifcfg-eth0 && echo "Clean ifcfg-eth0 ok!"
Here && indicates that the following command will only be executed after the previous command has been successfully executed.
and | | is to indicate that the previous execution was unsuccessful in executing the following command.
CD $NET _dir | | {
echo "cannot change to necessary directory," >&2
exit 1
}
Therefore, if you are not able to enter the directory, print the error message. &2 indicates a standard error, and &0 represents standard input. The &1 represents standard output. Script Execution:
The execution of the script will call the environment variables first, to find the relevant variables, and some variables are the system has been defined can be used directly, such as the UID used above.
Cat/etc/profile (Global) CAT/ETC/PROFILE.D (global) The variable can be placed directly in this area
. BASHRC: User Environment variables
three ways to execute a script:Bash or sh + script name (file has no executable permission, or no interpreter specified) absolute path/script name (requires execute permission) or./(similar to the first method, but requires executable permissions) source + script name or. Foot Name
>bash xxx.sh
>bash >xxx.sh
Bash or sh Specifies that the bash executes the script and then starts the child shell to execute. If you do not specify which script command to use in the first line of the script (' #!/bin/bash, can only be placed on the first line and placed on the other lines for comment processing), you need to use the first method. However, the Linux system is bash by default, so you don't have to specify it, but not on other systems.
>source xxx.sh
. xxx.sh
The source or. command is the shell's built-in command, which does not create a child shell, executes directly in the current Shell , and invokes the system's default shell to execute the script, regardless of whether the shell command parser is specified in the script.
So use. and SH run, the following behavior occurs:
Because. Does not start the child shell, so the statement is executed directly in the child shell, and the variable is passed to the parent shell. All new and changed statements in the script will be stored in the current shell. screenshot of execution result:
If root permission is not used:
After adding sudo:
Paste the Myip and mynetwork two files:
Myip
Device=eth0
hwaddr=00:0c:29:03:4d:ec #物理地址无需修改
bootproto=static
nm_controlled=yes #实时生效, No need to restart NIC immediately after modification
ipaddr=192.168.0.27
broadcast=192.168.0.255
netmask=255.255.255.0
gateway= 192.168.0.1
onboot=yes #开机启用网络配置.
type=ethernet
dns1=8.8.8.8
ipv6init=no
userctl=no
MyNetwork:
Networking=yes
Hostname=yangni
gateway=192.168.0.1 #添加网关地址