ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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.
(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).
...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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.