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
The
forloop is not designed to loop over lines. Instead it loops over words. Words are things separated by space. Lines are things separated by newline. More on that later.The idiomatic way to loop over lines is to use a
whileloop in combination withread:Alternatively:
Both work fine for most simple cases. The second variant is using a process substitution which might not be available in all shells.
Both variants have advantages and disadvantages which mainly become apparent if you want to manipulate variables inside the loop.
For more information see http://mywiki.wooledge.org/BashFAQ/024
technical nitpick:
words, or fields as they are called in bash, are things separated by space but also by tab and newlines. basically things separated by whitespace.
the separator separating the fields is defined in the
IFSvariable (short for Internal Field Separator). Usually$IFScontains a space, a tab, and a newline.Often you will see the suggestion to loop over lines by changing the value of
$IFSto only newline.(the
$'\n'is is called ANSI-C Quoting and might not be available in all shells)I do not recommend changing
$IFS. Many commands rely on sane setting for$IFS. Changing$IFSwill often cause an endless nightmare of obscure bug hunting.See also: