Some tips I get when learning bash
2004-12-06 16:02
423 查看
1.Space problem:
when using “test” , which is synonymous with “ [” , except that when the [ command is used, a trailing ] is also used just for readability. Spaces must be put between the [ braces and the condition being checked.
when using string comparision token “=“ in “[]“,space before and after “=“ is also requied(I made this error once, luckily my script was simple and I figured it out soon).
Now , I just remember this by thinking that “[ “is just like writing “test“, as “if test -f xxx“ is same as “if [ -f xxx ]“, and string comparison is just like arithmetic comparision “-eq“, so I should always add spaces.
But if u use "=" to assign a value to a variable, space shouldn't be added, or it'll incur a error like "gr1x : command not found" in statement " gr1x = "good guy" "
2. Variable Problem
Use quotes around variable as much as possible. Like in
>read input
>if [ $input = “gr1x” ]
if ur script user just press Enter, which means $input contains nothing, just black string, then bash parse it like this :
>if [ = “gr1x“ ]
so error occured “ [: =: unary operator expected“;
if ur script user input “aaa bbb ccc“, which means bash will parse it like this:
>if [ aaa bbb ccc = “gr1x“ ]
error occured : “[: = : too many arguments“
3. Control Structures
-----------
for variable in values
do
statements
done
-----------
if condition
then
statements
elif condition
then
statements
else
statements
fi
-------------
while condition do
statements
done
--------------
until condition #similiar to while , with the condition reversed
do
statements
done
---------------
case variable in
pattern [ | pattern] ...) statements;;
pattern [ | pattern] ...) statements;;
...
* ) statement 1 #use wildcard to make it default
statement 2 #the most explicit matches first and the most general match last
statement 3 #multiple statements
.............;; # this ;; can be omited as it's the last case
esac #interesting, just reverse the letter order like if-fi
----------------
statement1 && statement2 && statement3 && ... #The AND List
#To execute a series of commands, executing the next command only if all the previous commands have succeeded.
statement1 || statement2 || statement3 || ... #The OR List
#To execute a series of commands until one succeeds, then not execute any more commands following .
#Short Circuit Evaluation: only the minimum number of statements is executed to determine the result, statements that can’t affect the result are not executed.
4. Use braces {} to enclose multiple statements to make a statement segment
5. Always define a function before you can invoke it, no forward declarations in bash.
6. Funtion parameters: $*, $@, $#, $1, $2......When the function finishes, they are restored to their previous values---the positional parameters to the script.
Use "local" to declare local variable
(note:
$0-----The name of the shell script.
$#-----The number of parameters passed to script.
$$-----The process ID of the shell script,useful when generate /tmp/tmpfile_$$.
$1, $2, ...--------The parameters given to the script.
$@-----A list of all the parameters, in a single variable.
$*-----A list of all the parameters, in a single variable, separated by the first character in the environment variable IFS.
$IFS--- An input field separator;
)
7. Command :, the null command, can be used as an alias for true, which means "while ture" has the same meaning with "while :".
>if [ -d fred ]; then
> :
>else
> echo directory gr1x did not exist
>fi
8.The dot . command executes the command in the current shell, useful if u wanna to set enviroumental variable within a script,
as when a script executes an external command or script, a new environment (a subshell) is created,the command is executed in the new environment, and the environment is then discarded apart
from the exit code that is returned to the parent shell.
9.Command find: find [path] [options] [tests] [actions]
We can force the precedence of tests and operators by using parentheses,since these have a special meaning to the shell, we also have to quote the braces using a backslash.
if we use a pattern for the filename, we must use quotes so that the name is not expanded by the shell but passed directly to the find command.
For example:
>find . /( -name “_*” -or -newer bit /) -type f -exec ls -l {} /;
/; is the teminator of -exec or -ok, and {} is replaced with the full path to the current file, try omitting {} to see what happened.
when using “test” , which is synonymous with “ [” , except that when the [ command is used, a trailing ] is also used just for readability. Spaces must be put between the [ braces and the condition being checked.
when using string comparision token “=“ in “[]“,space before and after “=“ is also requied(I made this error once, luckily my script was simple and I figured it out soon).
Now , I just remember this by thinking that “[ “is just like writing “test“, as “if test -f xxx“ is same as “if [ -f xxx ]“, and string comparison is just like arithmetic comparision “-eq“, so I should always add spaces.
But if u use "=" to assign a value to a variable, space shouldn't be added, or it'll incur a error like "gr1x : command not found" in statement " gr1x = "good guy" "
2. Variable Problem
Use quotes around variable as much as possible. Like in
>read input
>if [ $input = “gr1x” ]
if ur script user just press Enter, which means $input contains nothing, just black string, then bash parse it like this :
>if [ = “gr1x“ ]
so error occured “ [: =: unary operator expected“;
if ur script user input “aaa bbb ccc“, which means bash will parse it like this:
>if [ aaa bbb ccc = “gr1x“ ]
error occured : “[: = : too many arguments“
3. Control Structures
-----------
for variable in values
do
statements
done
-----------
if condition
then
statements
elif condition
then
statements
else
statements
fi
-------------
while condition do
statements
done
--------------
until condition #similiar to while , with the condition reversed
do
statements
done
---------------
case variable in
pattern [ | pattern] ...) statements;;
pattern [ | pattern] ...) statements;;
...
* ) statement 1 #use wildcard to make it default
statement 2 #the most explicit matches first and the most general match last
statement 3 #multiple statements
.............;; # this ;; can be omited as it's the last case
esac #interesting, just reverse the letter order like if-fi
----------------
statement1 && statement2 && statement3 && ... #The AND List
#To execute a series of commands, executing the next command only if all the previous commands have succeeded.
statement1 || statement2 || statement3 || ... #The OR List
#To execute a series of commands until one succeeds, then not execute any more commands following .
#Short Circuit Evaluation: only the minimum number of statements is executed to determine the result, statements that can’t affect the result are not executed.
4. Use braces {} to enclose multiple statements to make a statement segment
5. Always define a function before you can invoke it, no forward declarations in bash.
6. Funtion parameters: $*, $@, $#, $1, $2......When the function finishes, they are restored to their previous values---the positional parameters to the script.
Use "local" to declare local variable
(note:
$0-----The name of the shell script.
$#-----The number of parameters passed to script.
$$-----The process ID of the shell script,useful when generate /tmp/tmpfile_$$.
$1, $2, ...--------The parameters given to the script.
$@-----A list of all the parameters, in a single variable.
$*-----A list of all the parameters, in a single variable, separated by the first character in the environment variable IFS.
$IFS--- An input field separator;
)
7. Command :, the null command, can be used as an alias for true, which means "while ture" has the same meaning with "while :".
>if [ -d fred ]; then
> :
>else
> echo directory gr1x did not exist
>fi
8.The dot . command executes the command in the current shell, useful if u wanna to set enviroumental variable within a script,
as when a script executes an external command or script, a new environment (a subshell) is created,the command is executed in the new environment, and the environment is then discarded apart
from the exit code that is returned to the parent shell.
9.Command find: find [path] [options] [tests] [actions]
We can force the precedence of tests and operators by using parentheses,since these have a special meaning to the shell, we also have to quote the braces using a backslash.
if we use a pattern for the filename, we must use quotes so that the name is not expanded by the shell but passed directly to the find command.
For example:
>find . /( -name “_*” -or -newer bit /) -type f -exec ls -l {} /;
/; is the teminator of -exec or -ok, and {} is replaced with the full path to the current file, try omitting {} to see what happened.
相关文章推荐
- Unix awk使用手册
- bash 详解 [转]
- Do All in Cmd Shell
- 用 curl 和 scsh 编写 web 脚本
- SQL注入得到WEBSHELL
- 溢出下shell下的文件上传
- Shell FAQ 以后累加
- [转载]BASH脚本编程速成
- Shell编程
- 利用shell编程实现DOS风格的Linux命令行
- 用MASM32 DIY的下次开机自动删除文件程序(v0.0.0005)
- PHP也可以當成Shell Script
- 批量编译shell
- VC的托盘程序
- 发表一下对COM的理解
- 怎样在osworkflow中使用BeanShell
- 初学者之路 -- IBM AIX OS 常 用 shell 指 令 集 (二)
- IE不能打开新窗口的解决办法
- Do All in Cmd Shell
- CU Play Shell 十三问之:var=value?export 前後差在哪?