TonyW
TonyW

Reputation: 18875

format lines using Shell scripts

I have a list of shell scripts (that end with .sh) in a folder and I am trying to make a list of them, and the list should have two script names (separated by a space) in each line. I wrote the following script, but it does not work without showing any errors. I was hoping to get some help here:

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

Upvotes: 0

Views: 113

Answers (3)

glenn jackman
glenn jackman

Reputation: 246774

The pr utility is handy for columnizing as well. It can split the data "vertically":

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

or "horizontally"

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

Upvotes: 2

William Pursell
William Pursell

Reputation: 212238

To get tab separated output:

  ls *.sh | paste - -

Upvotes: 3

Guntram Blohm
Guntram Blohm

Reputation: 9819

It's not a good idea to set i=1 everytime in your loop.

Try

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

Upvotes: 1

Related Questions