顯示具有 Bash 標籤的文章。 顯示所有文章
顯示具有 Bash 標籤的文章。 顯示所有文章

2011年10月14日 星期五

[Linux][Bash][Read Line to Get Each Element]

#!/bin/sh 

while read inputline 
do 
    RSUBDIR="$(echo $inputline | cut -d: -f1)" 
    VOL="$(echo $inputline | cut -d: -f2)" 
    
    echo RSUBDIR = $RSUBDIR and VOL = $VOL 
done < FILE_RSUBDIR_VOL

and file FILE_RSUBDIR_VOL is below

200808:20                                                            
200654:19
200387:18
200244:17

I am still trying to figure out how to distinguish different separators.

2011年10月13日 星期四

[Linux][Bash][用 wc 取得檔案行數]

之前單純使用 wc -l 時會在行數後面加上檔名,一直覺得很不喜歡。

所以想說用 sed 和 awk 來取得行數,指令如下:

wc -l FILE | sed -n "1 p" | awk -F, '{print $1}'

結果失敗了 XDDDDD

最後只用 cat FILE | wc -l 就得出了我想要的結果..真是哭笑不得。

2011年10月7日 星期五

2011年10月5日 星期三

[Linux][Bash][取檔案中的值]

Value1=`sed -n "$i p" $1 | awk -F, '{print $1}'`

在此 $i 是迴圈的變數。

第一個 $1 是命令列的第二個,為資料輸入的檔案。

第二個 $1 是 awk 中判斷讀入資料的第一個。

[Linux][Bash][For 迴圈寫法]

以 1~10 為例子.

1) for i in {1..10}
2) for i in $(seq 1 10)

[Linux][Bash][數學運算]

C=`echo "$A [+-*/] $B" | bc`
若是要進行浮點數計算時將 bc 改為 bc -l。

or

NUMBER=1
NUMBER=`expr $NUMBER + 1`

or

NUMBER=1
let "NUMBER=NUMBER+1"

[Linux][Bash][判斷 wget 有沒有抓到檔案]

WGET_OUTPUT=$(wget -q "URL")

if [ $? -ne 0 ]; then
    echo "Fail"
else
    echo "Success"
fi

[Linux][Bash][Unary operator expected]

wget_output=$(wget "URL")

if [ $wget_output == 0 ]; then
...

執行時在 if 那行會出現 unary operator expected 的訊息。

因為若 wget_output 沒拿到值的話,內容是 NULL, 所以在判斷式中沒有 lvalue。

此時可以將判斷式改成 if [ "$wget_output" == "0" ]。

或是 if [[ $wget_output == "0" ]],都可以順利解決這問題。

[Linux][Bash][Generate 001-005]

#!/bin/bash

for i in {1..5}
do
    Name=`printf %0.3d $i`
    echo $Name
done

=================================

After executing the executable file, the screen will display

001
002
003
004
005