语法基础
Shell 语法基础
Shebang
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/python
#!/usr/bin/python3
#!/usr/bin/env bash
脚本中所有的语句都会使用首行声明的解释器来进行执行,绝大部分的脚本都是以 #!/bin/bash
开始,这就保证了不管该脚本在何解释器中运行都会被/bin/sh
来执行。
/bin/sh
#! /bin/sh
### BEGIN INIT INFO
# Provides: policykit
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Create PolicyKit runtime directories
# Description: Create directories which PolicyKit needs at runtime,
# such as /var/run/PolicyKit
### END INIT INFO
# Author: Martin Pitt <martin.pitt@ubuntu.com>
case "$1" in
start)
mkdir -p /var/run/PolicyKit
chown root:polkituser /var/run/PolicyKit
chmod 770 /var/run/PolicyKit
;;
stop|restart|force-reload)
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
/usr/bin/env bash
/usr/bin/env
在修改后的环境中运行#/usr/bin/env bash
的优点是它将使用运行用户的 $PATH
变量中首先出现的
#!/usr/bin/env bash
# Purpose: Mount glusterfs at boot time
# Must run as root
# Author: Vivek Gite
# --------------------------------------
p='gfs01:/gvol01'
mount | grep -wq "^${p}"
if [ $? -ne 0 ]
then
mount -t glusterfs "$p" /sharedwww/
fi
注释
#!/bin/bash
# A Simple Shell Script To Get Linux Network Information
# Vivek Gite - 30/Aug/2009
echo "Current date : $(date) @ $(hostname)"
echo "Network configuration"
/sbin/ifconfig
以#开头的单词或行会导致该单词和该行上的所有剩余字符被忽略。这些行不是要执行
多行注释的定义方式如下:
#!/bin/bash
echo "Adding new users to LDAP Server..."
<<COMMENT1
Master LDAP server : dir1.nixcraft.net.in
Add user to master and it will get sync to backup server too
Profile and active directory hooks are below
COMMENT1
echo "Searching for user..."