Most of the sites come up with the while loop, but i found us `for` loop
$ ls -l
total 1392
-rw-------@ 1 sanjeev users 277935 May 17 18:21 Jenkins_Job_packaging.pdf
-rw-------@ 1 sanjeev users 227774 May 17 18:21 Pipeline Based Alert and Notification.docx
-rw-r--r--@ 1 sanjeev users 1324 May 17 18:21 SieveOfAtkin.js
-rw-r--r-- 1 sanjeev users 1301 May 17 18:21 VPN (Cisco IPSec).networkConnect
-rw-------@ 1 sanjeev users 187319 May 17 18:21 algo.docx
-rw-r--r-- 1 sanjeev users 3797 May 17 18:21 bill.feb
-rw-r--r-- 1 sanjeev users 65 May 17 18:22 i
With for loop it was bad output
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"
done
$ bash i
1: Jenkins_Job_packaging.pdf
2: Pipeline
3: Based
4: Alert
5: and
6: Notification.docx
7: SieveOfAtkin.js
8: VPN
9: (Cisco
10: IPSec).networkConnect
11: algo.docx
12: bill.feb
13: i
The spaces in filename is used as separators
$IFS
internal field separator
This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.
Improved for loop, using IFS
IFS=$'\n'
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"
done
And finally we get the correct filenames
$ bash i
1: Jenkins_Job_packaging.pdf
2: Pipeline Based Alert and Notification.docx
3: SieveOfAtkin.js
4: VPN (Cisco IPSec).networkConnect
5: algo.docx
6: bill.feb
7: i
Well IFS is well documented, but not popularly used .
No comments:
Post a Comment