You may as well use printf correctly anyway. Aside from possible issues with a leading dash with GNU coreutils and bash printf implementations, there would be problems if anything in the filename looks like a format specifier or escape sequence.
Regards,
Alister
Could you possibly educate me on how then? Feel free to PM me so we don't go off topic.
---------- Post updated at 02:34 AM ---------- Previous update was at 02:31 AM ----------
Quote:
Originally Posted by methyl
Forgot to mention that you should use double quotes when calling the script if your parameter contains space characters:
I believe in coding that type of flaw out. Hence the post.
---------- Post updated at 05:58 AM ---------- Previous update was at 02:34 AM ----------
My updated script... still having some issues
I keep getting errors like this.
I'm thinking it might have something to do with the % in the file/directory names.
Is that something that is best dealt with using sed?
You may as well use printf correctly anyway. Aside from possible issues with a leading dash with GNU coreutils and bash printf implementations, there would be problems if anything in the filename looks like a format specifier or escape sequence.
Quote:
Originally Posted by Binary Buddha
Could you possibly educate me on how then? Feel free to PM me so we don't go off topic.
Not only is it not off-topic, incorrect use of printf is the root of your problem.
Quote:
Originally Posted by Binary Buddha
The first argument to printf is a format string. A conversion specifier (aka format specifier) is a sequence of characters within a format string which begins with a %.
What will be the format of the output of those printf commands? It's impossible to say. You have handed over control of the format string to external sources. How printf will behave and how many arguments it will require depend on the type and number of conversion specifiers in the format string. The type and number of specifiers in turn depends on the variable values $FILE and $GENRE.
You want to be very careful about what you allow into that first argument to printf.
Quote:
Originally Posted by Binary Buddha
I keep getting errors like this.
I'm thinking it might have something to do with the % in the file/directory names.
You are correct.
Quote:
Originally Posted by Binary Buddha
Is that something that is best dealt with using sed?
No. There's no need to mangle the file names just to print them out. What you need to do is not allow arbitrary data into your format string.
Corrected printf statements:
Note how the format string is now invariant. Whatever the value of the variables, the format string never changes (a point driven home by switching to strong single-quotes).
The same bug lurks in your awk one-liner:
Quote:
Originally Posted by Binary Buddha
Make $2 an argument and in the format string replace it with an appropriate conversion specifier (%s in this case).
In shell scripting, this type of error is typically nothing worse than garbled output, but a format string bug in a language like C can be a major security issue. For more info, see Uncontrolled format string.
You can't use single quotes inside double quotes. They don't nest -- it's treated as the end of the single quote.
Awk doesn't use single quotes anyway. Use double quotes.
You can't use variables inside quotes in awk. Get rid of those quotes around $2.
You forgot the comma after the first argument, and the brackets.
You can also get rid of that grep by putting the regex inside awk itself. That's done really easily.
In fact, you can replace that entire enormous pipe-chain with it. awk is a whole programming language, not a glorified cut. And you can match the numbers in one regex instead of three by using ?, a specifier like * that means "zero or one of the previous character".
@Corona688 Think he wanted to strip leading whitespace, and a bracketed string of up to 3 digits from end of field 2 (he was using _ as the sed delimiter).
This slight change should cover it:
Last edited by Chubler_XL; 08-05-2012 at 09:56 PM..
These 2 Users Gave Thanks to Chubler_XL For This Post:
Hello
I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below:
ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Hi,
I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server.
e.g: filenames are
1) SHmail _profile001_20120908.txt
2) SHmail_profile001 _20120908.txt
3) sh... (3 Replies)
I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command?
find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file:
ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample
ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.
Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??... (7 Replies)
Hi
I hope someone will be able to resolve this little teaser!
I am running a script
for file in `ls directory`
do
echo "$file"
...other code here....
done
this works fine unless we receive a file with a name which has a space in it
ie
"filena me"
(I know its not good... (8 Replies)
Hi All,
I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory.
What I have:
... (4 Replies)
I have files on my unix boxes that users have created with spaces.
Example: /tmp/project plan
ls -l "/tmp/project plan" works fine.
$/tmp>ls -l "/tmp/project plan"
-rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan
I created a file called test and put just the... (2 Replies)
I'm trying to do something like that:
for $filename in `ls -1`
do
some_command $filename
done
but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
I have a problem with the script below
#!/bin/sh
for vo in `find -maxdepth 1 -type f -regex "^\./*$"`
do
ls -l "$vo"
some other commands
done
It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed
Any solutions? (4 Replies)