顯示具有 shell script 標籤的文章。 顯示所有文章
顯示具有 shell script 標籤的文章。 顯示所有文章

2014年1月10日 星期五

shell script 裡取絕對值

Last updated on Jan 10th, 2014 by Sam Lin <samlin35@gmail.com>


我臨時需要在 shell script 裡計算絕對值,經我參考一些網路上的文章與討論後,最後找到這樣的方法是可行的,寫成筆記並且跟大家分享。下面範例重點就在於 $price_down 這個變數後面使用 #- ,就可以將參數變成絕對值了。


My environment: Freebsd 9.2


$ cat abs_ex.sh
#!/bin/sh
htc2012=1300
htc2013=123
price_down=`expr $htc2013 - $htc2012`
echo price_down=$price_down
echo abs=${price_down#-}


$ ./abs_ex.sh
price_down=-1177
abs=1177

2013年10月1日 星期二

sh infinite loop

sh infinite loop

Oct 1th, 2013 by Chuan-Hsien Lin
Last Modified on Oct 1th, 2013

有時候我們在 unix-like 的環境底下,需要簡單重複某些動作,例如用在測試程式、或是檢查狀態等等,簡單的方法是使用 shell script 就可以做到這樣的無窮迴圈 (infinite loop),下面範例以 sh 為例,檔名取為 run.sh。

#!/bin/sh

while :
do
   echo "test"
   sleep 1
done

以上程式存檔之後,記得要改為可執行權限,使用 chmod +x run.sh 指令修改其權限。

然後在 command line 環境下執行 ./run.sh 即可。

執行結果如下,就會不停地重複 while 裡面的動作。

ingrasys:~# ./run.sh
test
test
test
test
test
test

...