2014-01-09 67 views
0

我有一個文件夾中的shell腳本(以.sh結尾)的列表,我試圖列出它們的列表,並且列表應該有兩個腳本名稱(用空格分隔)在每一行中。我寫了下面的腳本,但沒有顯示任何錯誤,它不起作用。我希望能在這裏得到一些幫助:使用Shell腳本的格式行

file1="" 

for file in $(ls ./*.sh); do 

    i=1 
    file1="$file1$file " 

    if [ $i -lt 3 ]; then 
     i=$((i++)) 
     continue 
    fi 

    echo "file1 is $file1"   # expected $file1 is: scriptName1.sh scriptName2.sh 
    echo $file1 >> ScriptList.txt # save the two file names in the text file 
    file1="" 
done 

回答

3

要獲得製表符分隔輸出:

ls *.sh | paste - - 
+0

粘貼爲+1,但不應該使用'ls',使用'printf'%s \ n「* .sh |粘貼 - '' –

+0

有沒有辦法讓「空間」分離輸出?謝謝 – TonyGW

+0

printf如何提供幫助?我喜歡使用'printf'%25s \ n「* sh'來幫助對齊,但沒有寬度說明符似乎並不重要。 –

1

這不是一個好主意,設置i=1每次在循環。

嘗試

ls -1 | while read a; 
do 
    if [ -z $save ]; then 
     save="$a" 
    else 
     echo "$save $a" 
     save="" 
    fi 
done 
+0

注意,當'的ls''的輸出通過管道或重定向-1'自動啓用。比較'ls'和'ls |貓' –

+0

但是,'ls>/dev/tty'會生成列:) –

2

pr效用是非常方便的過柱爲好。它可以分割數據「垂直」:

$ seq 10 | pr -2 -T -s" " 
1 6 
2 7 
3 8 
4 9 
5 10 

或「水平」

$ seq 10 | pr -2 -T -s" " -a 
1 2 
3 4 
5 6 
7 8 
9 10 
+0

非常好.......... –