LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-29-2013, 11:53 PM   #1
hDUQUE
Member
 
Registered: Jan 2007
Posts: 76

Rep: Reputation: 15
shell script issue


hey guys,
i'am sorry, this is not a fedora issue but i use to used this forum ... the question is about escaping the special blank character. Please take a look at this simple script:

Code:
echo 11111111111111111111111111111111
ls ./\[VOCALOID2\ SONiKA\]\ THE\ VOCALOIDS\ \(KRAFTWERK\'s\ THE\ ROBOTS\ Vocaloid\ Cover\)-KyIpSKp8lh0.mp3

echo 22222222222222222222222222222222
`ls ./\[VOCALOID2\ SONiKA\]\ THE\ VOCALOIDS\ \(KRAFTWERK\'s\ THE\ ROBOTS\ Vocaloid\ Cover\)-KyIpSKp8lh0.mp3 > tmp`
cat tmp

echo 33333333333333333333333333333333
for fileName in `cat tmp`
do 
	echo fileName= "$fileName"
done
and its execution:

Code:
hduque#./test.sh
11111111111111111111111111111111
./[VOCALOID2 SONiKA] THE VOCALOIDS (KRAFTWERK's THE ROBOTS Vocaloid Cover)-KyIpSKp8lh0.mp3
22222222222222222222222222222222
./[VOCALOID2 SONiKA] THE VOCALOIDS (KRAFTWERK's THE ROBOTS Vocaloid Cover)-KyIpSKp8lh0.mp3
33333333333333333333333333333333
fileName= ./[VOCALOID2
fileName= SONiKA]
fileName= THE
fileName= VOCALOIDS
fileName= (KRAFTWERK's
fileName= THE
fileName= ROBOTS
fileName= Vocaloid
fileName= Cover)-KyIpSKp8lh0.mp3
I am waiting to have THE SAME output for steps 1,2 and 3. The 3rd step doesn't interpret the escaped blank character ... i'am waiting for just one line into the for command. Like this:

Code:
fileName= ./[VOCALOID2 SONiKA] THE VOCALOIDS (KRAFTWERK's THE ROBOTS Vocaloid Cover)-KyIpSKp8lh0.mp3

does somebody knows whats going on ?

thanks in advance!!!

héctor
 
Old 04-30-2013, 01:35 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
(Unnecessary use of temporary files, unsafe tempfile creation,) "for" versus "while" loops:
Code:
doSomethingWith() { echo "$1"; }

find /some/directory -type f -iname \*.mp3 | while read ITEM; do
 doSomethingWith "${ITEM}"
done


---------- Post added 30-04-13 at 01:36 ----------

Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
1 members found this post helpful.
Old 04-30-2013, 03:32 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
In this instance you could still use a for loop, but as pointed out above, not by reading it from a file as this should almost always be done with a while loop.

Using for:
Code:
for fileName in *.mp3
do
    echo "file name = $fileName"
done
 
Old 04-30-2013, 04:06 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
There are a few common problems in scripting that are easy avoidable if you keep to simple rules, and one of them is using "for" instead of "while" loops. If you don't want to take it it from me, David The H. or other LQ members well-versed in scripting see http://mywiki.wooledge.org/DontReadLinesWithFor
 
Old 04-30-2013, 07:01 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Or even:
Code:
doSomethingWith() { printf '%s\n' "$1"; }
(It works even if the filename is ./-n and you use basename to strip ./)
 
Old 04-30-2013, 07:30 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Note "doSomethingWith()" means just that: do something. The most important thing to take away from this thread is using a "while" loop.
 
Old 04-30-2013, 07:31 AM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by hDUQUE View Post
Code:
`ls ./\[VOCALOID2\ SONiKA\]\ THE\ VOCALOIDS\ \(KRAFTWERK\'s\ THE\ ROBOTS\ Vocaloid\ Cover\)-KyIpSKp8lh0.mp3 > tmp`
In addition to what has already been said, what's the purpose of backticks here? They do absolutely nothing other than confuse people.

Quote:
Originally Posted by unSpawn View Post
Code:
doSomethingWith() { echo "$1"; }

find /some/directory -type f -iname \*.mp3 | while read ITEM; do
 doSomethingWith "${ITEM}"
done
Beware it'll break if file name contains new line characters (yeah, find is kinda useless that way).
 
Old 04-30-2013, 08:39 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(Those who use newlines in the file-names should be killed painfully anyways:|)
 
Old 04-30-2013, 08:50 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I agree. While it's a nice find, I've been bitten by new lines a few times, I dread the day we feel we need to include new lines in default exclusion clauses here.
 
Old 04-30-2013, 09:21 AM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by NevemTeve View Post
(Those who use newlines in the file-names should be killed painfully anyways:|)
Maybe so, but before they are killed, they may destroy your system if they use new lines for malicious purposes. Imagine the following being run via crontab with root privilages:
Code:
find /tmp -mtime +7 | while read file; do rm -rf "$file"; done
All one needs to do is: mkdir -p "$(printf '/tmp/\n/\n')" and than wait a week to delete all the files on the system.
(In this instance, find /tmp -mtime +7 -delete solves the issue).
 
Old 04-30-2013, 11:12 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by mina86 View Post
crontab with root privilages
...which 0) requires root rights to edit and therefore 1) an entry vector (let's forego talking about insider jobs), followed by 2) successful elevation of privileges and 3) the humor to "hide" stuff in roots crontab in the first place...
 
Old 04-30-2013, 11:50 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You have a point here, mina86, there should be a kernel-parameter/mount-option against such filenames.
 
Old 04-30-2013, 12:00 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
Originally Posted by unSpawn View Post
There are a few common problems in scripting that are easy avoidable if you keep to simple rules, and one of them is using "for" instead of "while" loops. If you don't want to take it it from me, David The H. or other LQ members well-versed in scripting see http://mywiki.wooledge.org/DontReadLinesWithFor
Sorry to be the stick in the mud here unSpawn, but if you look closer at my example you will see that I am not reading from a file nor reading lines so the example is
not relevant. Furthermore if we look at Mr. Wooledges site we will see the following http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29 indicates this is a
good way to read from a directory.
 
Old 04-30-2013, 12:13 PM   #14
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by grail View Post
Sorry to be the stick in the mud here unSpawn, but
No need to, it's perfectly fine to point out you've got a good solution to a problem.
+1
 
Old 04-30-2013, 12:15 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
GNU find (-print0) and bash (read -d '') can handle files with newlines:
Code:
#!/bin/bash
# list-files.bash
touch $'a\nfile' 'another file'

find . -print0 | while read -d '' file ; do
    echo "[$file]"
done
Code:
$ ./list-files.bash
[.]
[./a
file]
[./another file]
[./list-files.bash]
 
1 members found this post helpful.
  


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
Issue in running commands in expect script from shell script yadvinder Programming 0 05-31-2012 04:07 AM
Shell Script issue elmo219 Linux - Newbie 4 03-09-2012 10:08 AM
Issue with shell script zaeem Linux - Networking 10 01-06-2011 09:56 AM
Shell script issue suvra82002 Linux - Enterprise 23 07-26-2008 02:02 PM
issue with shell script chupacabra Linux - General 3 10-18-2002 08:12 PM

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

All times are GMT -5. The time now is 03:14 PM.

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