Some tips I get when learning bash

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值