Problems with script that unzips multiple zipped tar files then untars the same files in C shell


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Problems with script that unzips multiple zipped tar files then untars the same files in C shell
# 1  
Old 04-01-2020
Problems with script that unzips multiple zipped tar files then untars the same files in C shell

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.

Code:
#!/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


Last edited by odensun; 04-02-2020 at 02:42 AM..
# 2  
Old 04-02-2020
Have you considered using another shell? See:
The if keyword is case sensitive, fix If on line 5.
# 3  
Old 04-02-2020
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.
# 4  
Old 04-02-2020
Using tcsh version 6.22.01, with no matching files the script appears to be working correctly:

Code:
$ ./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.
# 5  
Old 04-03-2020
Thanks man, i'll give this a try.
# 6  
Old 04-03-2020
If you really must use csh, then try to make it more robust.
Code:
#!/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

# 7  
Old 04-03-2020
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help getting a UNIX shell script to look at multiple files.

Hey everyone! I made a shell script that would go through a file and replace any phrase or letter with another phrase or letter. Helps update variable names or values. The following code is this: #!/bin/sh Word1="$1" Replace1="$2" File1="$3" arg=$( echo "$Word1" | sed 's:\:\\&:g' )... (3 Replies)
Discussion started by: rebmonk
3 Replies

2. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

3. Shell Programming and Scripting

shell script to ftp multiple files

Hi, i use the below script to send a single file to remote server from linux. ftp -nvi <<!EOF open $Host_name user $USER_ID $PWD binary mput $file_name quit !EOF (where i... (2 Replies)
Discussion started by: pradebban
2 Replies

4. Shell Programming and Scripting

Need shell script for ftpying multiple files

Hi All, I am a beginner for shell programming. I have a requirement to ftp multiple files. Here are the details. I have around thiry files in one directory, I want a shell script which selects 5 files at a time and does ftp them to another host . After the transfer for first files is... (0 Replies)
Discussion started by: srikanthn
0 Replies

5. Shell Programming and Scripting

How to Read Multiple files in a Shell Script

Hi, Can any one tell me if i can read two files in a shell script... My actual requirement is to read the 1st text file and parse it to get the file code and use this file code to retrieve data from database and print the fetched data in the 2nd text file (I have parsed it and printed the... (2 Replies)
Discussion started by: funonnet
2 Replies

6. Shell Programming and Scripting

To write a shell script which groups files with certain pattern, create a tar and zip

Hi Guru's, I have to write a shell script which groups file names based upon the certain matching string pattern, then creates the Tar file for that particular group of files and then zips the Tar file created for the respective group of files. For example, In the given directory these files... (3 Replies)
Discussion started by: rahu_sg
3 Replies

7. Shell Programming and Scripting

shell script to join multiple files

I am a new to Linux and try to write a script to join three multiple files. For example, there are three files file1 # comment a Kevin b Vin c Sam file 2 # comment a 10 b 20 c 40 file 3 # comment a blue b yellow (7 Replies)
Discussion started by: bonosungho
7 Replies

8. UNIX for Advanced & Expert Users

How to create a Tar of multiple Files in Unix and FTP the tar to Windows.

Hi, On my Unix Server in my directory, I have 70 files distributed in the following directories (which have several other files too). These files include C Source Files, Shell Script Source Files, Binary Files, Object Files. a) /usr/users/oracle/bin b) /usr/users/oracle... (1 Reply)
Discussion started by: marconi
1 Replies

9. Shell Programming and Scripting

bash - batch script for extracting one file from multiple tar files

so i have hundreds of files named history.20071112.tar (history.YYYYMMDD.tar) and im looking to extract one file out of each archive called status_YYYYMMDDHH:MM.lis here is what i have so far: for FILE in `cat dirlist` do tar xvf $FILE ./status_* done dirlist is a text... (4 Replies)
Discussion started by: kuliksco
4 Replies

10. UNIX for Dummies Questions & Answers

Tar multiple files

Hi All, There 2 files in the folder /temp/tst/ 1.txt 2.txt When I run the command find /temp/tst \( -name "*.txt" \) -exec tar cf /temp/123.tar {} \; it creates the tar file 123.tar with only one file in it and that is 2.txt.But if I use the command find /temp/tst \( -name "*.txt"... (1 Reply)
Discussion started by: rony_daniel
1 Replies
Login or Register to Ask a Question