This one bothers me a lot why ls command output is considred as unsafe?
Code:ls -l | something ls -l | command | do | get_output
This one bothers me a lot why ls command output is considred as unsafe?
Code:ls -l | something ls -l | command | do | get_output
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...
![]()
Never send a boy to do a mans job.
it will NOT be "unsafe" if you know what you are doing. If you are parsing files, don't use ls -l because you might hit problems with files with spaces in their file names.
a lot depends also on what your "something" or "command" is.... what do you actually want to do?? describe the problem you are having.
rockdalinux (1st January 2010), vamsi (30th December 2009)
The ls will display all files including newlines, commas, pipes and almost anything except for NUL (\0). By default ls separates its output with newline (\n). This is good, but imagine your filename has newline (\n) itself. This makes it pretty difficult to get filename safely, hence it is considered as unsafe. Also, some output fields in the ls command changes from UNIX to Linux oses. Try using while or for loop to get file list. Here is a common for loop I use (again as ghostdog74 said provide us more info as we cannot imagine your problem):
While example:Code:find /path/to/dir -type f -print0 | while IFS= read -r -d '' file do echo "Processing ${file} done
See http://www.cyberciti.biz/tips/handli...s-in-bash.htmlCode:find . -print0 | while read -d $'\0' file do echo -v "$file" done
Last edited by nixcraft; 31st December 2009 at 09:50 AM.
All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]
rockdalinux (1st January 2010)
I never had problem coz I never had a blank or anything special in file name. But just now I tested with blank and other stuff and it did failed. I was comparing some file using ls and sending to our program written in python. thanks for sharing for and while loop. I will patch my script later on![]()
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...
![]()
Never send a boy to do a mans job.
There is no problem parsing the output of ls -l unless there is a newline in a filename. Whitespace is no problem.
Symlinks need to be treated specially.
Code:ls -l | while read -r perms links owner group size month day time file do case $file in *\ -\>\ *) : parse link name and file name here ;; esac : do whatever done
There are currently 1 users browsing this thread. (0 members and 1 guests)