2020年8月13日 星期四

if 比較,字串與數值 的 operator 不同

ref: How to Compare Numbers, Strings and Files in Bash Shell Script

就是 if [ A ... 裡面 用的 operator,在數值字串會不一樣..
以上面的 link 中的說明:

if [2 -gt 3]
     then
     print "2 is greater"
     else
     print "2 is not greater"
fi
其他的 數值用 operator:
  • num1 -eq num2 : check if 1st number is equal to 2nd number
  • num1 -ge num2 : checks if 1st number is greater than or equal to 2nd number
  • num1 -gt num2 : checks if 1st number is greater than 2nd number
  • num1 -le num2 : checks if 1st number is less than or equal to 2nd number
  • num1 -lt num2 : checks if 1st number is less than 2nd number
  • num1 -ne num2 : checks if 1st number is not equal to 2nd number

字串的話...
name=linuxtechi
if [ $USER = $name ]
        then
                echo "User exists"
        else
                echo "User not found"
fi
  • var1 = var2 checks if var1 is the same as string var2
  • var1 != var2 checks if var1 is not the same as var2
  • var1 < var2 checks if var1 is less than var2
  • var1 > var2 checks if var1 is greater than var2
  • -n var1 checks if var1 has a length greater than zero
  • -z var1 checks if var1 has a length of zero

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 的值。

2015年1月7日 星期三

example : disk usage 超過90% , 刪除Android folder


ref: http://stackoverflow.com/questions/19703621/get-free-disk-space-with-df-to-just-display-free-space-in-kb

2014年10月13日 星期一

刪除括號 (和括號內的文字)

$ echo '0 [123] 456' | sed 's/\[.*\]//' 
0  456

就是.. 把 [.*] switch 成 empty.
因為 [,] 要 escapre,所以變成 \[.*\]

把他放到 s/pattern/replace/action 裡的 pattern.

2014年5月5日 星期一

ssh no password

就是連線A server 時,不需要輸入 password.

方法是把你的 public key 先送給 A server,告訴他用這個 public key 來驗證你將來 ssh client 給的key.
驗證 OK 就代表是真正的你在要求連線。就不用輸入password


首先,先產生自己的key, 會有一對,private, public:
$ssh-keygen -t rsa

都直接回 return 的話,會產生兩個 file 在 .ssh 中:
  • id_rsa : private
  • id_rsa.pub : public

然後把 pub key 送到 A server 去:
 $scp .ssh/id_rsa.pub a_server:~


最後,就是到 A server 去,把這個 public key 加到 authorized_keys:
 $cat id_rsa.pub >> .ssh/autorized_keys


* 要注意,cat key 出來看,可以看到最後是 username/servername

這個跟跟連線時的 username/servername 要對應。
-- 在 hosts 是自己亂寫的時候,要特別注意。

debian 的話,自己的機器在 ssh-keygen 完,產生 id_rsa pair 後,還要下
 $ ssh-add
Identity added: /home/charles-chang/.ssh/id_rsa (/home/charles-chang/.ssh/id_rsa)


否則 ssh A server 時,會出現:
Agent admitted failure to sign using the key.


ref:

2013年12月24日 星期二

cron 的一些.. run as XXX 還有環境

cron 是定時執行的 daemon。
命令是在; /etc/crontab
在crontab 中可以指定以哪一個 user 的身份來 run command
但是並不包含該 user 環境變數。

所以 run 起來會和真的以該user run 該 script 的結果不同。

為了解決這個問題,
可以在 command script 的開頭加入: . .profile

test result . 判斷執行成功或失敗

在 shell script 中,需要知道這個 command 執行成功或失敗。
$?
來代表

例如: cd mydir if [ "$?" -ne "0" ]; then echo failed else echo success fi
0 代表成功。



另外,有要用 pipe log ,又要判斷的情況。
例如: make -j4 2>&1 | tee makelog 這時候用 $? 就會是 tee 這個 command 執行的結果,不是 make 的

在bash, 可以用 ${PIPESTATUS[0]} 來代表 make -j4 2>&1 | tee makelog if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo failed else echo success fi

2013年1月3日 星期四

sed -- 取出 pattern 中的某個部份

例如:從 XML 檔中取出某 tag 的值。
<name>Charles</name> <id>123</id> <color>red</color> ...

用 sed 來操作,就是...
  • 找到 符合 pattern 的地方
  • 印出 pattern 的某個部份

以上面的例子,xml tag 的 pattern 和 value就是: <.*> \(.*\) <\/.*>
前後被 tag 包起來,中間的部份要留下來 \(.*\)

把這個放到 sed 控制字串中: sed 's/ <.*>\(.*\)<\/.*> / \1 /' file 一樣用 s 指令。
最後用 \1 代表 pattern 中,第一個 \(.*\) 的內容。

這樣所有的 tag 都會被取出。
如果只是要某個 tag,例如 : id。
就可以把 id 寫入 pattern 中: sed 's/ <id>\(.*\)<\/id> / \1 /' file





2012年10月2日 星期二

string array

類似 array 的作法,在 shell script 中是用 space 作分別。
像: STRINGLIST="a b c_d e,f" for ele in $STRINGLIST do echo $ele done
結果output 就是: a b c_d e,f

所以要一個一個加string array 內容是.. SARY="a" SARY=$SARY" b" SARY=$SARY" c" echo $SARY
結果會是: a b c

2012年8月20日 星期一

create large file with dd command

要一個大的 file 作 loop back。
要先 create。

以往都是用: dd if=/dev/zero of=10G bs=1G count=10 這樣會花一些時間,硬碟把 00 寫入 10G 的 file。


要是不管file 的內容,只要10G 大小,可以用: dd if=/dev/zero of=10G bs=1 count=0 seek=10G 這樣馬上就可以create 出來。


  • http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/

2012年7月23日 星期一

copy files according the list file

其實這是 moore 的 script。
他把各系統要 copy 的 filename 寫在同一個 list file 中,每個 filename 用 ≶ > 刮起來。
仿造 xml 格式,裡面是 系統名稱。
<N_Series>MyBrowser.apk</N_Series>

所以要寫一個 script,依照這個 list 內容來 copy files..




要注意那個從 filelist xml 中剔除 tag 的 sed,要先把 </...> 剔除,再剔除開頭的。
否則因為整個 entry 剛好被包圍,所以會被整個剔除。

2012年7月22日 星期日

mount , 並指定 uid, gid 和 charset

mount 並且設定 mount folder 的 uid, gid, (or iochartset).

都是用 -o

uid, gid:
# mount -o uid=charles-chang,gid=charles-chang /dev/sdd1 ~/sd

如果是要 mount windows 的 share (samba, cifs),中文的問題可以用: iocharset:
mount -t cifs -o username=softwaretest,password=softwaretest,iocharset=utf8,uid=charles-chang,gid=charles-chang //192.168.147.225/ODM3 /home/charles-chang/buildingmachine

兩個 -o 可以何在一起,中間用 ',' 隔開

2012年5月17日 星期四

find .. then cd to it(them)

找到含 .git 的目錄,cd 過去,run git status..

ref: http://www.linuxquestions.org/questions/programming-9/shell-script-to-cd-into-the-result-of-find-592503/

#/bin/sh find . -type d -name '.git' | while read F; do D=$F/../ cd "$D" echo $PWD git status cd - > /dev/null echo --------------- done 找到的 path 含 .git,所以要cd 到 .git 上一層。
做完後,回到剛剛的目錄: cd - 不要把 cd - 的 command 印(echo) 出來,所以加上 > /dev/null

2012年4月24日 星期二

string operation -- replace file extension name

如果只是簡單的filename 操作,可以用 "${filename...}"
例如:把所有 mp4 檔 rename 成 avi: for file in *.mp4;do mv "$file" "${file/.mp4/.avi}";done
像這個 ${file/.mp4/.avi} 就是把 $file 中 .mp4 換成 .avi,其中的兩個 "/" 符號就是 replace。


ref:
  1. http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
  2. http://www.faqs.org/docs/abs/HTML/string-manipulation.html