0

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

3 Answers 3

3

To get tab separated output:

  ls *.sh | paste - -
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for paste, but you shouldn't use ls, use printf "%s\n" *.sh | paste - -
is there a way to get "space" separated output? thanks
How does printf help? I like the idea of doing printf "%25s\n" *sh to help with alignment, but without a width specifier it doesn't seem to matter.
To change the delimiter to space, use paste -d ' ' - -
2

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

Comments

1

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

2 Comments

Note that -1 is automatically enabled when the output of ls is piped or redirected. Compare ls with ls | cat
However, ls > /dev/tty will produce columns :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.