I've been trying to write a script that will search a directory for multiple tar files that are zipped, unzip them, then untar them. However my problem is that everytime I run the script it will execute the first if then statement, but it won't execute the second one. I've ran them both statements in separate scripts and they both work independently, however not when ran in the same script. Do I need to combine them in a if then elseif statement? Here's what I have so far, I appreciate any help.
#!/bin/csh
set x = (`find /Documents/*.gz -print`)
set v = (`find /Documents/*.tar -print`)
if ("$x" != "") then
gunzip *
else
echo "No zip files found."
endif
if ("$v" != "") then
foreach i ( $v )
tar -xvf $i
end
else
echo "No tar files found."
endif
Have you considered using another shell? See:
The if
keyword is case sensitive, fix If
on line 5.
My bad about the capital I on line five. That was a mistake when I typed the code for the post. My actual script didn't have this. Regarding using another shell though, I wish I could, however because of the server i'm working with, it has to be in c shell. Thank you the help though.
Using tcsh version 6.22.01, with no matching files the script appears to be working correctly:
$ ./odensun
find: No match.
find: No match.
No zip files found.
No tar files found.
I suspect the gunzip *
command is not returning control to your script. This command does look suspicious, I would have expected gunzip $v
in it's place. This is trying to unzip every file and directory in the current directory.
Thanks man, i'll give this a try.
If you really must use csh, then try to make it more robust.
#!/bin/csh
# ` ` is whitespace-separated and subject to expansion, but in list context you can "` `" that is newline-separated and no further expansion
set x = ( "`find /Documents/*.gz -print`" )
set v = ( "`find /Documents/*.tar -print`" )
# always have $var in " " (also enforces a string or 1st array member), or use $var:q or ${var:q}
if ("$x" != "") then
gunzip $x:q
else
echo "No zip files found."
endif
if ("$v" != "") then
foreach i ( $v:q )
tar -xvf "$i"
end
else
echo "No tar files found."
endif
I appreciate the help. I tried implementing the changes you suggested, however no dice yet. I'm sure what the problem is. It's like the shell just won't recognize the second if-then statement. It keeps telling me "no tar files found" despite them being there. I'm not sure if this is because the script is running both these statements at the same time and can't recognize that the zipped files are now tar files, or if there's just a syntax problem in the code itself. I need to do more research regardless. Thanks again.
Run it in debug mode:
either put set echo
in the script (and unset echo
to turn it off again).
Or run the script with csh -x scriptname
I finally got it working. Thank you for all the help. I appreciate it.