Bash Script handling arguments with spaces
location: ubuntuforums.com - date: March 9, 2007
What's the proper way to pass/handle arguments to a bash script that have spaces in them. Right now I'm using $* to get all the arguments. This fails when escaping the spaces or protecting the arguments in quotes. Here's the relavent code
for FILE in $*
cp $FILE $TMPDIR
Make bash script process filenames with spaces in it ?
location: ubuntuforums.com - date: October 28, 2005
I'm very new at bash scripting and just got my first script, a file mass renamer, to work.
Now I'd like to expand it by making it accept input filenames that contain spaces.
So far I've figured out a way to turn ' ' into '\ ' with sed :
sed -e "s/ /\\\ /g"
But no matter what I do I can't seem to get mv to accept this as input (ie as the source file).
This is what I'm trying to do :
for i in *; do
source=$(echo $i|sed -e "s/ /\\\ /g")
mv $source $target
With $source, like above mv interprets an input like fi\ le\ 1 as multiple files.
When I do "$source" instead it gets interpreted as fi\\ le\\ 1.
Any ideas ?
Is there a better way to do this ?
bash pass multiple arguments with spaces
location: linuxquestions.com - date: April 6, 2009
I have a c program that takes multiple arguments (filenames). You can pass in as many arguments as you want. You can run it like,
./myCprogram "My File1" "My File 2"
Now I want to call that from a bash script. My bash script needs to read the arguments from command line and pass them to the c program. I can read the arguments fine but I can't pass them correctly because the arguments have spaces in them.
Right now I am doing something like,
#Echo prints "My File1" "My File 2"
This fails. Bash puts ` in spaces.
Then I tried
This fails too as the whole $myargs is passed as single parameter to myCprogram.
How can I fix this.
Passing arguments with spaces between (bash) script
- date: January 1, 1970
I've got the following bash two scripts
./b.sh 'My Argument'
The someApp binary receives $* as 2 arguments ('My' and 'Argument') instead of 1.
I've tested several things:
Running someApp only thru b.sh works as expected
Iterate+echo the arguments in b.sh works as expected
Using [email protected]
instead of $* doesn't make a difference
Command line arguments with spaces to a C program through shellwrapper script
location: linuxexchange.com - date: July 14, 2012
what does it take to make my program accept command-line arguments with spaces?
Yet-another EDIT: I have just recognized that the program is started from a shell-script that sets up the environment for the execution of the program. As there are some external libraries, LD_LIBRARY_PATH is set to the current working directory:
The issue is definitely related to $*. Is there any solution to correctly forward the command-line parameters?
Here is a code-snippet from main():
if (stat(argv, &lStat) != 0)
fprintf(stderr, "Cannot stat(2) given file: %s. \n", argv);
I am starting my program with the following parameters:
./data_sniffer /mnt/pod/movies/some\ test\ movie\ file.avi
The resulting error message looks like this:
Cannot stat(2) given file: /mnt/pod/movies/some.
Anyone an idea what's wrong here? I think that I am
Perl script that has command line arguments with spaces
location: linuxexchange.com - date: August 12, 2009
I'm feel like I'm missing something pretty obvious here, but I can't seem to figure out what's going on. I have a perl script that I'm calling from C code. The script + arguments is something like this:
my_script "/some/file/path" "arg" "arg with spaces" "arg" "/some/other/file"
When I run it in Windows, Perl correctly identifies it as 5 arguments, whereas when I ran it on the SunOS Unix machine, it identified 8, splitting the arg with spaces into separate args.
Not sure if it makes any difference, but in Windows I'm running it like:
perl my_script <args>
While in Unix I'm just running it as an executable like show above.
Any idea why Unix is not managing that argument properly?
Here's the code for calling the perl script:
char *script = "my_script";
char *argument = "\"arg1\" \"arg2\" \"arg3 with spaces\" \"arg4\" \"arg5\"";
sprintf( cmd, "%s %s >1 /dev/null 2>&1", script, arguments);
system( cmd );
That's not exactly it, as I
bash, cp and filename (with spaces) woes
location: linuxquestions.com - date: June 9, 2006
I am trying to get a script to move files from one dir to another when they are older than 180 days. What I have so far:
Bash script calling makefiles with CFLAGS prepended
location: linuxquestions.com - date: February 6, 2010
I am trying to automate my linux build setup from a Bash script.
Most all of this works when I execute it manually, but I cannot figure out how to get a bash script to execute it automatically.
In this particular case, I am trying to build the xorg utilities. If I manually step through the process, prepending the commands with CFLAGS, LDFLAGS, etc, all of the packages build.
So I created a bash script, test.sh:
Bash: loop over directories with spaces in their names
location: ubuntuforums.com - date: January 23, 2010
Okay, so I installed Debian, but when I moved my music collection back onto my computer from the external hard drive, somehow the permissions got all messed up. So I want to make a script that changes all the directories back to 755 and the flac files to whatever the hell is right for them.
The problem is, all the directories (well, most) and files have spaces in their names. For example, "Kate Bush/The Dreaming/1 - Sat in Your Lap.flac". So I can't get my Bash loop to work right. I was hoping to do something like
for ARTIST in `ls -d *`
chmod -v 755 $ARTIST
for ALBUM in `ls -d *`
chmod -v 755 $ALBUM
for TRACK in `ls *`
chmod -v 644 $TRACK
Unfortunately, ls ends up going nuts with the spaces present. What's the best way to make this work with the spaces?
Bash script for usage with nagios
location: ubuntuforums.com - date: November 10, 2011
I'm using a script made by someone else.
It is used to read the CPU-usage of my Bluecoat_SG Proxy.
It works great apart from the output once implemented...
The maker suggested there might be a problem with the syntax of the logical comparison.
The code goes as follows:
if [ $performanceData -ge $criticalTreshold ]; then
if ([ $performanceData -lt $criticalTreshold ]&&[$performanceData -ge $warningTreshold ]); then
So...my given warningTreshold is 80 (%) and my CriticalTreshold is 90 (%).
The script/plugin reads out the value...(differs but let's say in this time it is) 55% (cpu-usage).
Then it still gives me a WARNING in the output, while it should say OK.
Does anyone of you see any errors in this piece of work?
I tried (( instead of ([ and also >= instead of -ge and so on...
Thanks in advance!
Ps. The maker state
Page: 1 2 3 4 5 6 7 8 9 10