Hello all,

I have one shell script that takes the filename for processing as an argument. for eg. ./BulkEdit.sh SampleFile.xls. In the script it reads the filename from "$1" and does the processing of that particular file read from command line. If i have space character in the file name, script doesnt get the file name properly and skips its processing. for eg. ./BulkEdit.sh Sample File.xls. It gives the error message as File "Sample" doesnot exist.

I have also tried the following things and it doesnt work.

./BulkEdit.sh "Sample File.xls"
./BulkEdit.sh 'Sample File.xls'
./BulkEdit.sh Sample\ File.xls

Please suggest me how it can be read.


Thanks,
Jasmine

If you can quote your parameters:

% cat s
#!/usr/bin/sh

ls -l "$1"
% ./s "pip po"
-rw-r--r-- 1 sysadmin None 0 Sep  4 11:46 pip po

Otherwise (assuming single parameter):

% cat s
#!/usr/bin/sh

ls -l "$*"
% ./s pip po
-rw-r--r-- 1 sysadmin None 0 Sep  4 11:46 pip po

Let me explain the problem in detail.

I have the java program from which i am calling the script called BulkEdit.sh.

Process proc2= rt.exec("sh /usr/web/sites/admtool/jakarta-tomcat-5.0.28 /webapps/admintool/BulkEdit.sh \'"+pathFileName+"\' &");

where as path file name as /usr/web/sites/admtool/jakarta-tomcat-5.0.28/webapps/admintool/upload/Keyword Update.xls

Please note that there is a space between Keyword and Update in the XLS file. The actual format during the execution of the java program is as follows.

sh /usr/web/sites/admtool/jakarta-tomcat-5.0.28 /webapps/admintool/BulkEdit.sh "/usr/web/sites/admtool/jakarta-tomcat-5.0.28/webapps/admintool/upload/Keyword Update.xls"

The content of the BulkEdit.sh is as follows.

more BulkEdit.sh
echo $1 >> jmj.txt

$1 has the value of ""/usr/web/sites/admtool/jakarta-tomcat-5.0.28/webapps/admintool/upload/Keyword. Because of the space in the file name, it is not recognizing the filename properly in the script. But if i run the script directly from the server not thru the Java file with the same command line input of space in the filename, then the script is echoing the correct file name. Can you please help me on why it happens only calling from Java program?

Thanks,
Jasmine

Try changing the shell script to:

echo "$#" >> jmj.txt

Thanks for your quick reply. It returns the value as 3 not the file name.

Thanks,
Jasmine

Hi!

I think Rad' meant to put quotes around the variable, like "$1". I don't think that's going to help though. This problem used to frustrate me so much!

Okay, I'm not sure how this will translate to what you're doing because I don't know java, but I usually get around this in bash with "read". Here's an example... I start with a test file that has multpile lines with spaces:

$ cat test.txt 
hello kitty
scooby doo
mighty mouse
casper the friendly ghost
yogi bear

We can read lines out of this file with something like "cat", but we'll have the same problem you're seeing which is that each line gets broken into a new line at the spaces:

$ for i in `cat test.txt`; do echo "$i"; done
hello
kitty
scooby
doo
mighty
mouse
casper
the
friendly
ghost
yogi
bear

To get around this, we can use "read" in a loop:

cat test.txt |while read LINE; do 
  N=$((N=1))
  echo "$LINE"
done

This gives us the output that we want. For your script, you might try something like changing the IFS (Internal Field Seperator) variable (and changing it back when we're done):

OLDIFS="$IFS"
cat test.txt |while IFS= read LINE; do 
  echo "$LINE"
done
IFS="$OLDIFS"

So, like I said, I'm not 100% sure how this would work in your script, but here's a few things to try!

Thanks,
-G

Thanks for your quick reply. It returns the value as 3 not the file name.
[...]

Yep, sorry!
It was a typo,
I meant:

echo "$*" >> jmj.txt
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.