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: