Visit Our UNIX and Linux User Community


Problem with shell script while spaces encountered in directory names


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Problem with shell script while spaces encountered in directory names
# 1  
Old 04-21-2016
Problem with shell script while spaces encountered in directory names

Hi,
I am having issues with the jar -tf command when I put in the shell script.

The command runs fine from the command line as shown below.

Code:
[root@quickstart Documents]# jar -tf "./VirtualBox Dropped Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar"

But when I put in a shell script(shown below) and the directory name has spaces.In this case "Virtual Box Dropped" has spaces.It gives an error as shown below.

----test1.sh code------
Code:
#!/bin/bash
for jar in `find . -iname '*.jar'`;do
 jar -tf "$jar" > cls.dat
done

Code:
[root@quickstart Documents]# ./test1.sh localrunner

Error:

Code:
java.io.FileNotFoundException: ./VirtualBox (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)
java.io.FileNotFoundException: Dropped (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)
java.io.FileNotFoundException: Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)

I tried to put the double quote but it doesnt work?

Any ideas?

Thanks


Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-21-2016 at 05:23 PM.. Reason: Added code tags, also around error output
# 2  
Old 04-21-2016
Try enclosing the "command substitution" in double quotes.

Last edited by RudiC; 04-21-2016 at 05:40 PM..
# 3  
Old 04-21-2016
Quote:
Originally Posted by vinoo128
Hi,
I am having issues with the jar -tf command when I put in the shell script.

The command runs fine from the command line as shown below.

Code:
[root@quickstart Documents]# jar -tf "./VirtualBox Dropped Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar"

But when I put in a shell script(shown below) and the directory name has spaces.In this case "Virtual Box Dropped" has spaces.It gives an error as shown below.

----test1.sh code------
Code:
#!/bin/bash
for jar in `find . -iname '*.jar'`;do
 jar -tf "$jar" > cls.dat
done

Code:
[root@quickstart Documents]# ./test1.sh localrunner

Error:

Code:
java.io.FileNotFoundException: ./VirtualBox (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)
java.io.FileNotFoundException: Dropped (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)
java.io.FileNotFoundException: Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:116)
    at sun.tools.jar.Main.list(Main.java:1004)
    at sun.tools.jar.Main.run(Main.java:245)
    at sun.tools.jar.Main.main(Main.java:1177)

I tried to put the double quote but it doesnt work?

Any ideas?
Thanks
Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!
Hello vinoo128,

Could you please try changing your code to as follows and let me know if this helps you.
Code:
 #!/bin/bash
for jar in *.jar
 do
   jar -tf "$jar" > cls.dat
done

because anyways find is looking for the current directories files only. Let me know how it goes for you.

Thanks,
R. Singh
# 4  
Old 04-21-2016
Quote:
Originally Posted by RavinderSingh13
.
.
.
Code:
 #!/bin/bash
for jar in *.jar
 do
   jar -tf "$jar" > cls.dat
done

This will not help with the quoted file having spaces in its name ("VirtualBox Dropped Files"). Enclose *.jar in double quotes.

Quote:
because anyways find is looking for the current directories files only.
.
.
.
You have to take extra measures to confine find to the uppermost directory.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-21-2016
Why use a shell loop? Try with find's -exec

Code:
find . -iname '*.jar' -exec jar -tf {} \; > cls.dat


---
Quote:
Originally Posted by RudiC
This will not help with the quoted file having spaces in its name ("VirtualBox Dropped Files"). Enclose *.jar in double quotes.
[..]
Actually it does, because Ravinder used double quotes around the variable expansion "$jar", so spaces will be preserved and *.jar must not be put in double quotes, because the glob expansion will not work then.
Ravinder's approach is correct, but it only works for the current directory...

It even works with multi-line file names:
Code:
$ for i in ./a\ * ; do printf "file:%s:end\n" "$i"; done
file:./a  t:end
file:./a  y:end
file:./a b:end
file:./a multiline file
name.out.txt:end
file:./a z:end

compare with find:
Code:
$ find . -name "a *" -exec printf "file:%s:end\n" {} \;  
file:./a  t:end
file:./a  y:end
file:./a b:end
file:./a multiline file
name.out.txt:end
file:./a z:end
file:./fromdir/a  hg hg:end


Last edited by Scrutinizer; 04-22-2016 at 02:21 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-24-2016
Rats, yes. Apologies, RavinderSingh13!
# 7  
Old 07-20-2016
First: `man find` shows there are options for "UNUSUAL FILENAMES" (e.g. filenames with spaces).
Code:
      -print0
              True;  print the full file name on the standard output, followed by a null character (instead of the newline char‐
              acter that -print uses).  This allows file names that contain newlines or other types of white space  to  be  cor‐
              rectly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

Second: using a "for loop" may be dangerous, there can be more results than a Unix line can handle. I would prefer a while loop.
Code:
find . -iname '*.jar' -print0 |while read jar
do
  jar -tf "$jar" >> cls.dat
done

Third: Scrutinizer already mentioned to use the -exec option of the `find` command. To my opinion this is the best method.

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting/using part of directory names in script

I'm making a shell script to: -copy directories to a new location -perform conversions on the files within the copied directories -move the newly created files to a new directory Please see my super basic script and notes below... and thank you thank you thank you in advance !! ... (1 Reply)
Discussion started by: kayzee
1 Replies

2. Shell Programming and Scripting

Shell Script to Enter a Directory (Folder Name Contains Spaces)

I am writing a shell script to cd to a folder. folder name is "October Scripts" I am currently storing the name of folder in a shell variable as so path="October\ Scripts/". If I do cd $path I receive the error bash: cd: October\: No such file or directory What am I doing wrong? Kindly... (3 Replies)
Discussion started by: anand2308
3 Replies

3. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

4. Shell Programming and Scripting

problem switching to directory using shell script

Hi, I have an issue with switching to other directory through shell script. I have used cd /Music" but that doesn't help me. Then I have also tried using alias proj 'Music' alias then I get the error permission denied but i set the chmod 777 Music. I am using cShell for my... (3 Replies)
Discussion started by: baluk
3 Replies

5. Shell Programming and Scripting

Problem with blank spaces in shell script

Hi, I did the following script: DIR=`pwd`;for i in `find . -name GESTION -type d`;do cd $i/..;setfacl -R -m g:directores:rwx, GESTION;echo $DIR'/'$i;cd $DIR;done This code do the following actions: 1. Starting inside a folder, it's searching for any folder called "GESTION" 2. Every time... (3 Replies)
Discussion started by: argie01
3 Replies

6. UNIX for Dummies Questions & Answers

Problem with spaces in directory path

Hi Gurus, I have a requirement. cat /usdd/Sample/"NDDF Plus DB"/"NDDF Descriptive and Pricing"/"NDDF BASICS 3.0"/"Pricing"/1.txt | sed 's/*|*/|/g' | sed 's/^*//'| sed 's/^*//; s/*$//' > temp.txt In unix prompt the above command is reading the file 1.txt and I am... (1 Reply)
Discussion started by: prabhutkl
1 Replies

7. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

8. Solaris

Encountered a strange problem with doing a restore

Hi Unix Guru's, I have encountered a strange problem with doing a restore on a local sunu machine. We have 2 live system (v880, v450) and 1 test system v440. All the machines are sun4u and using sun os 8.0 We have 2 backup device. One Bcakup device connected with v880 and another backup... (0 Replies)
Discussion started by: cpandian
0 Replies

9. Shell Programming and Scripting

Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be... (5 Replies)
Discussion started by: Shakey21
5 Replies

Featured Tech Videos