顯示具有 BashScripts 標籤的文章。 顯示所有文章
顯示具有 BashScripts 標籤的文章。 顯示所有文章

2013年7月3日 星期三

bash for-counting-loop


在shell script中

for (( i=0 ; i<5; i++ ))
do
      # do something
done


這種for counting loop是在bash才有 ,sh沒有

2013年5月1日 星期三

常見script語言是否得加上"分號(semicolon)"來當作"語句(statement)"結束之總整理


script與萬惡的"分號(semicolon)"的淵源


在用putty寫scripts很多時候都會被萬惡的分號(;)給搞混,會心想說我現在到底在寫哪一種語言?!根據每種scripts的語言特性,整理一下到底有哪些scripts是必須得加上"分號(;)"的。

bash script

不用分號,在bash中指令的執行不須加上分號(;),分號只是用來代表指令的依序執行而已。

python

不用分號,在python的世界當中是不推薦使用分號當statement的結束

php

必須得加上分號當作statement的結束。

perl

必須得加上分號當作statement的結束。


ruby

不須加上分號,因為Ruby 則是根據 shell 的傳統,例如 sh 及 csh。同一行上的多項敘述必須以分號來分隔,但並不需要在該行結尾處使用,換行字元 (linefeed) 在此的作用與分號相同。如果該行以反斜線 (\) 結尾,則可忽略之後的換行字元,如此能讓單一邏輯程式敘述行 (logical line) 橫跨數行。

JavaScript

可加可不加,習慣上,JavaScript 程式敘述結尾會加上分號(;),表示一個完整敘述的結束。其實分號並不是必要的,只要每一句程式敘述皆不同行,是可以省略分號的,不過為了程式的完整性,以及日後維護程式方便,建議您最好還是養成每一行結尾都加上分號的習慣。

2013年4月24日 星期三

2013年4月10日 星期三

bash "&&" "||" operator


expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

bash "=~" operator


簡單來說,我們在bash script中的fi statement想加入regular expression時,則可以利用"=~"這個binary operator以及"[["、"]]"來撰寫。例如:

Syntax:   if [[  =~ regex ]]

if  [[ ${var} =~ "eth[0-9]" ]]; then
        # do something....
fi


An additional binary operator, ‘=~’, is available, with the same precedence as ‘==’ and ‘!=’. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3))


Return Value

The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression’s return value is 2



Examples
 # primary parameter checking
    if [[ ! ${dev} =~ eth[0-9] ]]; then
        echo "Error: parameter 'dev' format is not correct: ${dev}"
        exit 1
    elif [[ ! ${vlan} =~ "-*[0-9]" ]]; then
        echo "Error: parameter 'vlan' format is not correct: ${vlan}"
        exit 1
    elif [ -z ${netname} ]; then
        echo "Error: parameter 'netname' format is not correct: ${netname}"
        exit 1
    fi


Reference
[1] http://www.gnu.org/software/bash/manual/bashref.html
[2] http://stackoverflow.com/questions/2348379/use-regular-expression-in-if-condition-in-bash

bash script中的 let 應用範例



Reference
[1] let和expr的經典應用
     http://www.51testing.com/?uid-219209-action-viewspace-itemid-96723

2013年3月26日 星期二

sed vs find



http://liwecan.pixnet.net/blog/post/29606145-%5Blinux%5D-%E6%90%9C%E5%B0%8B%E4%B8%A6%E5%8F%96%E4%BB%A3-%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6%E8%A3%A1%E9%9D%A2%E7%9A%84-%E6%9F%90%E5%80%8B%E5%AD%97%E4%B8%B2


# grep的使用技巧
http://ottoshare.blogspot.tw/2012/06/linux.html

2013年3月20日 星期三

script的執行方式




./ 要看第一行的 #! 說用什麼intepreter執行
sh 的話就真的是用 sh 執行; 若用python就代表用python執行 ; 若用bash就代表用bash執行。

2013年3月9日 星期六

exec



  1. http://imxfox.appspot.com/?p=118002
  2. exec家族比較, http://man7.org/linux/man-pages/man3/execl.3.html
  3. http://tc.itkee.com/os/detail-1bae.html
  4. http://linux.about.com/od/commands/l/blcmdln_exec.htm


#include <unistd.h>

extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);


int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);

getent - get entries from Name Service Switch libraries



getent - get entries from Name Service Switch libraries

getent database [key ...]

   The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.  If one or more key arguments are provided, then only the entries that match the supplied keys will be displayed.  

Otherwise, if no key is provided, all entries will be displayed (unless the database does not support enumeration).