LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script issue (https://www.linuxquestions.org/questions/programming-9/shell-script-issue-4175460103/)

hDUQUE 04-29-2013 11:53 PM

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

unSpawn 04-30-2013 01:35 AM

(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.

grail 04-30-2013 03:32 AM

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


unSpawn 04-30-2013 04:06 AM

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

NevemTeve 04-30-2013 07:01 AM

Or even:
Code:

doSomethingWith() { printf '%s\n' "$1"; }
(It works even if the filename is ./-n and you use basename to strip ./)

unSpawn 04-30-2013 07:30 AM

Note "doSomethingWith()" means just that: do something. The most important thing to take away from this thread is using a "while" loop.

mina86 04-30-2013 07:31 AM

Quote:

Originally Posted by hDUQUE (Post 4941593)
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 (Post 4941646)
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).

NevemTeve 04-30-2013 08:39 AM

(Those who use newlines in the file-names should be killed painfully anyways:|)

unSpawn 04-30-2013 08:50 AM

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.

mina86 04-30-2013 09:21 AM

Quote:

Originally Posted by NevemTeve (Post 4941872)
(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).

unSpawn 04-30-2013 11:12 AM

Quote:

Originally Posted by mina86 (Post 4941905)
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...

NevemTeve 04-30-2013 11:50 AM

You have a point here, mina86, there should be a kernel-parameter/mount-option against such filenames.

grail 04-30-2013 12:00 PM

Quote:

Originally Posted by unSpawn (Post 4941714)
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.

unSpawn 04-30-2013 12:13 PM

Quote:

Originally Posted by grail (Post 4942019)
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

ntubski 04-30-2013 12:15 PM

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]



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