Help Modify call to functions depending on file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Modify call to functions depending on file name
# 1  
Old 01-07-2014
Help Modify call to functions depending on file name

Hi,

I am creating a script that does gunzip/unzip/untar depending on the file extension.

I have created 3 functions in my script and call them in following order.
1) gunzip function
2) unzip function
3) untar function

I am using
Code:
FILENAME=${FILENAME%.*}

to modify filename between functions.

If the file name I receive is "mdm_ind_usa.2031.fsacm.tar.zip.gz" this script works fine.

But it does not work if the file name is like this "mdm_ind_usa.2031.fsacm.zip.tar.gz" or any other order of zipping.
I would like to modify my call to 3 functions depending on the extension of the file name.

I could use below code. But this code I have to put it 6 times all to capture all permutations and combinations.
Code:
 
#----------------- Check for Tar File -------------# 
if `ls -ltr ${FILEDIR}/${FILENAME}| grep .tar$ > /dev/null 2>&1` then
call_untar # (untar function call)
elif `ls -ltr ${FILEDIR}/${FILENAME}| grep .zip$ > /dev/null 2>&1` then
call_unzip # (unzip function call)
elif `ls -ltr ${FILEDIR}/${FILENAME}| grep .gz$ > /dev/null 2>&1` then 
call_gunzip # (unzip function call)
else
echo "text file"
fi

Help is appreciated to modify my call to functions depending on the file name in a more efficient manner.
# 2  
Old 01-07-2014
Try sth along this line:
Code:
fn="mdm_ind_usa.2031.fsacm.tar.zip.gz"
ext=${fn##*.}
case "$ext" in
   gz)   call_gunzip;;
  tar)   call_untar;;
  zip)   call_unzip;;
esac

and loop the case statement until all extensions are used up.
# 3  
Old 01-07-2014
Need help with looping please.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script. of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script. how do i... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Split file depending on Column Value

Hi , my file look likes below , cat file.csv 12/09/2014,50,5,0,300 12/09/2014, ,5,0,300 12/09/2014,50,,,300 i need to split file , the first one contains values (2nd column is 50 , 3rd and fourth column is null ) the second file contains all others firstfile ... (2 Replies)
Discussion started by: ubaisalih
2 Replies

3. Shell Programming and Scripting

How to Split File to 2 depending on condition?

Hi , cat myfile.txt ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.2.50 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 !... (6 Replies)
Discussion started by: OTNA
6 Replies

4. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

5. Programming

can a linux kernel module call libc functions?

can a linux kernel module call libc functions, such as printf(), strcpy(), etc...? (9 Replies)
Discussion started by: vistastar
9 Replies

6. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

7. Shell Programming and Scripting

Insert text into file depending on variable

Hey guys , i have a variable with the contents ... NUMBER=4 and a test file with the contents 1248 1213 1214 1278 1200 3045 3444 2130 I want to execute a script that will produce the following output ( based on NUMBER=4) to be ... create 1248 (1 Reply)
Discussion started by: theshams
1 Replies

8. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies

9. Shell Programming and Scripting

split file depending on content

Hi, I have a file which contains records of data. I need to split the file into multiple files depending upon the value of last field. How do i read the last field of each record in the file??? Regards, Chaitrali (4 Replies)
Discussion started by: Chaitrali
4 Replies

10. Programming

call functions from diferents programs

hi i have ten program in C, and there are functions what are in all the programs. so, i want to make a directory to store all the functions what are in all the programs, and call them from the C programs. (sending variables and values) is that possible?¿? how ca i do that?¿? any idea,... (1 Reply)
Discussion started by: DebianJ
1 Replies
Login or Register to Ask a Question