2020年7月5日 星期日

bookmark : shell script 中的各種括號..

為避免這摸好的說明不見,所以轉貼一下.. (其實就是 copy 人家的內容...)

雙 (( 用在運算上..
((var++))
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))
用說括號 (( 刮起來的變數,不必加 $

方括號 [ 用在判斷
$ VAR=2
$ if [ $VAR -eq 2 ]
> then
> echo 'yes'
> fi
yes

接著的這個說明我不了解...
兩格個方括號用在 ? extended function ? 在regular expression 的 =~ 上使用.
$ VAR='some string'
$ if [[ $VAR =~ [a-z] ]]; then
> echo 'is alphabetic'
> fi
is alphabetic

大括號 { 用在區分變數名稱
$ foo='stage'
$ echo $fooone
           ... returns empty line
$ echo ${foo}one
stageone
大括號同時還有處理變數內容的功能..
$ var="abcdefg"; echo ${var%d*}
abc

程式輸出轉為變數

其實這跟一般shell script call shell command (ls. find ..etc) 然後處理輸出是一樣的。
不會因為是自己寫的程式而有不同,無腦的example 來看一下...
echo10.c:
#include <stdio.h>

int main()
{
 printf("%d",10);

 return 0;
}
然後 shell script 吃這個輸出..
#!/bin/bash

LOOPEND=$(./echo10)
echo "LOOPEND: ${LOOPEND}"

i=1
while [[ $i -le $LOOPEND ]]
do
 echo "$i"
 ((i = i + 1))
done
就很直覺使用..

反而是括號使用有點...參考一下..

2020年6月24日 星期三

check soft link exist

-L 用來檢查 softlink 在不在。
#!/bin/bash
if [ -L bbb ]; then
   echo OK
fi
如果 ls .. 中..
bbb --> aaa
..
不管 aaa 在不在,都會 OK。

2020年6月23日 星期二

eval

有時候在 shell script 中,有沒有 用 eval,姊果好像都一樣。
例如 f_list = $(eval ls "$1") 其中,如果 eval 刪掉 f_list = $(ls "$1") 姊果 f_list 的內容相同。

看到 這一篇 說明,決的得很清楚:
1) foo=10 x=foo
2) y='$'$x
3) echo $y
4) $foo
5) eval y='$'$x
6) echo $y
7) 10

2019年4月12日 星期五

ffmpeg, rotate all the videoes in folder

#!/bin/bash
for f in *.MP4; do
 ffmpeg -i "$f" -vf "transpose=1" "r${f%.MP4}.MP4"
done

2015年6月9日 星期二

操作字串

ref: http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/string-manipulation.html

就直接 copy ...

Substring Removal

${string#substring}
Strips shortest match of $substring from front of $string.

${string##substring}
Strips longest match of $substring from front of $string.

stringZ=abcABC123ABCabc
#       |----|
#       |----------|

echo ${stringZ#a*C}      # 123ABCabc
# Strip out shortest match between 'a' and 'C'.

echo ${stringZ##a*C}     # abc
# Strip out longest match between 'a' and 'C'.

反過來:

${string%%substring}
Strips longest match of $substring from back of $string.
stringZ=abcABC123ABCabc
#                    ||
#        |------------|

echo ${stringZ%b*c}      # abcABC123ABCa
# Strip out shortest match between 'b' and 'c', from back of $stringZ.

echo ${stringZ%%b*c}     # a
# Strip out longest match between 'b' and 'c', from back of $stringZ.

2015年2月12日 星期四

return value from shell script

ref: http://tldp.org/LDP/abs/html/exit-status.html

是用 exit 指令。
exit 123
然後 caller 用 $? 就可以取得上一個 script exit 的值。