Following is the shell script to read all the DSF present in the box. But since the line is having spaces, it is displaying them in different lines. For those of you who dont understand ioscan -m dsf, replace it by ls -ltr, then the output is such that the permission and names are displayed in different line, but i want them in the same line.

#!/usr/bin/ksh

for a in `ioscan -m dsf`
do
 echo  $a
done
link|improve this question

77% accept rate
feedback

1 Answer

The for loop is not designed to loop over lines. The input is split using the characters in the $IFS variable as separators. Usually $IFS contains a space, a tab, and a newline. That means the for loop will loop over the "words", not over the lines. One way to loop over lines is to change the value of $IFS to only newline. If you do this then you have to save the old value of $IFS and restore that after the loop. A better way is to use the a while loop in combination with read.

Change $IFS to only newline:

OLDIFS=$IFS
IFS=$'\n'
for line in $(ioscan -m dsf)
do
  echo $line
done
IFS=$OLDIFS

or use a subshell to contain the change to $IFS:

(
  # changes to variables in the subshell stay in the subshell
  IFS=$'\n'
  for line in $(ioscan -m dsf)
  do
    echo $line
  done
)
# $IFS is not changed outside of the subshell

Better: use a while loop with read:

ioscan -m dsf | while read line
do
  echo $line
done

See also:

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.