LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2007, 02:11 PM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
Wrong parsing of ls command in bash scripting


lets say I want to do in one folder ls :
Code:
ls | grep -i slackware | head -4
(Ebook)\ -\ Html\ -\ Linux\ Configuration\ &\ Installation\ (Slackware).zip
(ebook)\ -\ Slackware\ Linux\ Unleashed,\ Third\ Edition.zip
(ebook-PDF)\ Slackware\ Linux\ Essentials\ (edonkeylinks.com)\ [f.zip
(ebook-html)\ -\ SAMS\ -\ Slackware\ Linux\ Unleashed\ -\ Kamran\ Hus.zip
for that matter, if I for example want to move all these files to a temp folder, I would have done that :
Code:
zuki@WhiteCastle slackware #for i in $(ls | head -4); do mv $i tmp; done
mv: cannot stat `(Ebook)\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `Html\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `Linux\\': No such file or directory
mv: cannot stat `Configuration\\': No such file or directory
mv: cannot stat `&\\': No such file or directory
mv: cannot stat `Installation\\': No such file or directory
mv: cannot stat `(Slackware).zip': No such file or directory
mv: cannot stat `(ebook)\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `Slackware\\': No such file or directory
mv: cannot stat `Linux\\': No such file or directory
mv: cannot stat `Unleashed,\\': No such file or directory
mv: cannot stat `Third\\': No such file or directory
mv: cannot stat `Edition.zip': No such file or directory
mv: cannot stat `(ebook-PDF)\\': No such file or directory
mv: cannot stat `Slackware\\': No such file or directory
mv: cannot stat `Linux\\': No such file or directory
mv: cannot stat `Essentials\\': No such file or directory
mv: cannot stat `(edonkeylinks.com)\\': No such file or directory
mv: cannot stat `[f.zip': No such file or directory
mv: cannot stat `(ebook-html)\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `SAMS\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `Slackware\\': No such file or directory
mv: cannot stat `Linux\\': No such file or directory
mv: cannot stat `Unleashed\\': No such file or directory
mv: invalid option -- \
Try `mv --help' for more information.
mv: cannot stat `Kamran\\': No such file or directory
mv: cannot stat `Hus.zip': No such file or directory
because it doesn't parse the filename correctly.
so I tried ls -N but it's still doesn't parse it correctly :
Code:
zuki@WhiteCastle slackware #for i in $(ls -N | head -4); do echo $i; done
(Ebook)
-
Html
-
Linux
Configuration
&
Installation
(Slackware).zip
(ebook)
-
Slackware
Linux
Unleashed,
Third
Edition.zip
(ebook-PDF)
Slackware
Linux
Essentials
(edonkeylinks.com)
[f.zip
(ebook-html)
-
SAMS
-
Slackware
Linux
Unleashed
-
Kamran
Hus.zip
same for echo "$i"



any solution? it's just an example of why I need this command, I want to use it in my daily bash scripting.


thanks.
 
Old 04-24-2007, 03:42 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Try
Code:
for i in "$(ls)"; do echo "$i"; done
Dave
 
Old 04-24-2007, 04:01 PM   #3
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ilikejam
Hi.

Try
Code:
for i in "$(ls)"; do echo "$i"; done
Dave
Thanks! it works/
 
Old 04-25-2007, 10:23 AM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by itz2000
for that matter, if I for example want to move all these files to a temp folder, I would have done that :
Code:
[B]zuki@WhiteCastle slackware #for i in $(ls | head -4); do mv $i tmp

Code:
n=0
for file in *[Ss][Ll][Aa][Cc][Kk][Ww][Aa][Rr][Ee]*
do
  mv "$file" tmp
  n=$(( $n + 1 ))
  [ $n -eq 4 ] && break
done
Or:

Code:
set -- *[Ss][Ll][Aa][Cc][Kk][Ww][Aa][Rr][Ee]*
mv "$1" "$2" "$3" "$4" tmp
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM
Apache parsing css files for php scripting. dickohead Programming 1 09-14-2006 04:40 AM
Bash Scripting - echo command question gbhil Programming 2 04-27-2005 07:27 PM
Bash command scripting otisthegbs Linux - General 6 07-06-2004 12:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration