Scripting

Programming Fundamentals
Programming a yo and treatment
performed sequentially executed cyclically run the
shell programming: process show, interpreted
shell script: The first line shebang mechanism
#! / bin / bash
# / usr / bin / Python!
# / usr / bin / perl!
Use shell script: automation commonly used commands, perform system administration and troubleshooting
create a simple application, processing or text file
bash -n detection script syntax error
bash -x display the results step by step

env export declare -x lists all the environment variables
Variable Assignment: name = 'value'
 can use the reference value
the shell programming: the process shown, interpretive execution (1) may be a direct string: name = "the root"
(2) variable reference : name = "$ the USER"
(. 3) command reference: name = COMMAND
name = $ (the cOMMAND)
 variable reference: $ {name} or $ name
"" weak references, wherein the variable reference is replaced with the variable value
'strong references, which will not be replaced with variable reference variable value, while maintaining the original string
 displays all defined variables: the SET
 delete a variable: unset name
/etc/profile.d/env.sh variable write configuration file
echo $ BASHPID view the current environment
() parentheses can open a child process
{} braces can not open a child process,
echo $ the RANDOM

Environment variables
Read-only variables
variables readonly name can not be changed readonly
readonly -p view read-only variable
declare -r name set read-only variables
172.18.0.7 account wang passwd: magedua

==! = Right of the symbol can use wildcards, the variable name suggestions left double quotes
= ~ right of the symbol may be a regular expression, the left side of the variable name suggestions double quotes

Use the exit process report:
IF
elif -o or
fi

#! / usr / bin / the bash
IF [$ # -eq 0]; the number of arguments equal to the then 0 no parameters
echo "Usage: / root / batchusers userfile" system outputs
exit 1 Exit then returns the value given
end fi Input file not found
if [! -f "$ 1" ]; then determine if the file is not a file
echo "the Input file not found"
exit 2 exit the return value is given
at the end Fi

for user in `cat $1` 循环创建 cat 文件内容 把里面内容读出
Do
id  $user &>/dev/null   判断   结果无论对错都不要
echo "user $user already exists "  已经存在
useradd $user -s /bin/false      创建
echo "user $user is created."   创建好给出提示
Done 结束循环

Guess you like

Origin blog.51cto.com/14451238/2428512