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月4日 星期六
bash: 算術運算
bash中,算術運算有四種方式:
1. expr
2. $(())
3. $[]
4. let
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/techdoc/shell/c860.html
1. expr
2. $(())
3. $[]
4. let
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/techdoc/shell/c860.html
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日 星期三
bash script利用exec作I/O重導入當stdin來讀檔
exec < filename # 利用exec來做I/O重導入以當成stdin
while read line
do
echo $line # 一行一行的印出
done
http://blog.longwin.com.tw/2012/06/shell-read-file-cat-line-2012/
2013年4月20日 星期六
Associative Arrays in Bash Scripts
http://stackoverflow.com/questions/688849/associative-arrays-in-shell-scripts
http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
http://www.gnu.org/software/bash/manual/html_node/Arrays.html
http://stackoverflow.com/questions/6660010/bash-how-to-assign-an-associative-array-to-another-variable-name-e-g-rename-t
http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
http://www.gnu.org/software/bash/manual/html_node/Arrays.html
http://stackoverflow.com/questions/6660010/bash-how-to-assign-an-associative-array-to-another-variable-name-e-g-rename-t
2013年4月13日 星期六
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.
&& 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
2013年4月9日 星期二
列出CitrixXenserver 所有的VLAN tag (以遞增的方式排序)
xe vlan-list params=tag --minimal | awk 'BEGIN{FS=","} {for(i=1;i<NF;i++) print $i}' | sort -n | uniq
2013年4月2日 星期二
sed進階應用
sed 's/被取代的字串/要取代的字串/'
sed -r 's/^(admin_token) = (.*)/\1 = '要代換的token'/g'
\1: 代表暫存,然後在要取代的字串中取回
-r , regular expression
# sed的examples
http://www.theunixschool.com/2012/06/sed-25-examples-to-delete-line-or.html
http://www.grymoire.com/Unix/Sed.html
2013年3月28日 星期四
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
- http://imxfox.appspot.com/?p=118002
- exec家族比較, http://man7.org/linux/man-pages/man3/execl.3.html
- http://tc.itkee.com/os/detail-1bae.html
- 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).
訂閱:
文章 (Atom)