Having trouble writing a basic shell program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having trouble writing a basic shell program
# 1  
Old 11-30-2006
Having trouble writing a basic shell program

Hello. I'm trying to write a shell script that will take files that have .tar, .tar.gz, .tar.Z, .gz, .Z and .zip file extensions and uncompress and unarchive them. The script should be able to take multiple arguments. So far I can write a script using the case command that will do this but it will only take one argument. Also, I need to make it so that if it does not have one of those file extensions then it will return an error message. I think I need to use either a for or while loop and shift to do this but I'm just not getting it for some reason. Any ideas? I appreciate greatly any help anyone might have on this.
# 2  
Old 11-30-2006
Btw,.. for the record, here is what I have so far:

#!/bin/bash
# Program: uncomp

filelist=

while [ "$#" -ge 1 ]
do
filelist="$filelist $1"
shift
done

for var in $filelist
do
tar -xvf "$var"
done
# 3  
Old 11-30-2006
This might get you started....
Code:
$ cat bashfiles
#! /usr/local/bin/bash

for arg ; do
        case $arg in
        *.tar)
                echo $arg is a tar file
                ;;
        *.Z)
                echo $arg is a compressed file
                ;;
        *.gz)
                echo $arg is a gzipped file
                ;;
        *)
                echo $arg is an unsupported file type
                ;;
        esac
done
exit 0
$ ./bashfiles a b c d e f g h i j k  a.tar b.gz c.Z
a is an unsupported file type
b is an unsupported file type
c is an unsupported file type
d is an unsupported file type
e is an unsupported file type
f is an unsupported file type
g is an unsupported file type
h is an unsupported file type
i is an unsupported file type
j is an unsupported file type
k is an unsupported file type
a.tar is a tar file
b.gz is a gzipped file
c.Z is a compressed file
$

# 4  
Old 11-30-2006
Quote:
Originally Posted by Perderabo
This might get you started....
Code:
$ cat bashfiles
#! /usr/local/bin/bash

for arg ; do
        case $arg in
        *.tar)
                echo $arg is a tar file
                ;;
        *.Z)
                echo $arg is a compressed file
                ;;
        *.gz)
                echo $arg is a gzipped file
                ;;
        *)
                echo $arg is an unsupported file type
                ;;
        esac
done
exit 0
$ ./bashfiles a b c d e f g h i j k  a.tar b.gz c.Z
a is an unsupported file type
b is an unsupported file type
c is an unsupported file type
d is an unsupported file type
e is an unsupported file type
f is an unsupported file type
g is an unsupported file type
h is an unsupported file type
i is an unsupported file type
j is an unsupported file type
k is an unsupported file type
a.tar is a tar file
b.gz is a gzipped file
c.Z is a compressed file
$


Thanks man! That is awesome! Thank you so much. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Trouble setting up basic user authentication on apache2 web server

Hey guys! So I decided to set up some basic user authentication on my apache2 server, and I am running into some problems. I followed the documentation provided by apache on their website, but I cant create the password file for some reason. I did a little trouble shooting myself, and found... (40 Replies)
Discussion started by: LinuxIntern445
40 Replies

2. Programming

Trouble compiling program using the readline library.

Hi: in the info page for readline library I read -- Function: void rl_variable_dumper (int readable) Print the readline variable names and their current values to `rl_outstream'. If READABLE is non-zero, the list is formatted in such a way that it can be made part of an... (1 Reply)
Discussion started by: stf92
1 Replies

3. Shell Programming and Scripting

Having trouble using expect to launch vpn program

Hello all. I am a linux and linux scripting newbie so please forgive my ignorance. I have been tasked to write what I thought should be a pretty simple script that does the following: 1) Loads our forticlient ssl vpn command line client 2) sends the vpn password to the client Pretty... (2 Replies)
Discussion started by: wblakenc
2 Replies

4. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

5. Homework & Coursework Questions

Need help with Basic Unix Program

I am a newbie to UNIX. I'm learning UNIX on my own, just trying to get the jerk of how things work in UNIX environment. I am familiar with Windows environment. Can anyone pls write simple 'envprint' programs to : 1) List all the environment Information (using the -l or --l options) 2) ... (1 Reply)
Discussion started by: agup17
1 Replies

6. Programming

writing a shell program in ubuntu

Hi all, I have an assignment from school to write a shell program in linux. the idea is to exercise fork() and execv() functions.. the shell program is supposed to be the master and every command that the user prints will run in a new process. we also need to try running the command in every... (1 Reply)
Discussion started by: r3vive
1 Replies

7. HP-UX

Program monitor on BT-Basic

Hi, The "program monitor" command in BT-Basic prompt you for a user name and a password. How can I grant access only to certain users ? Thank you. (0 Replies)
Discussion started by: fosiceanu
0 Replies

8. Programming

Basic multithreaded program

I'd like to write a program (I'm flexible on language; C/C++ was my original idea but a scripting language would probably be better) that runs hundreds of programs, but only N = 4 (say) at a time. The idea is to keep all the cores on a multicore machine busy. How can I do this? In particular,... (6 Replies)
Discussion started by: CRGreathouse
6 Replies

9. Programming

Basic questions on writing a Unix Service (newbie help!)

Hi there. I've got 12 years experience writing C++ on Windows, and 3 years C# on Windows. Now my boss wants me to write a C++ app to run on Unix as a multithreaded 'service' (i.e. a program that runs with no user intervention). Some quick questions for The Experts: * Whats the best C++... (3 Replies)
Discussion started by: Rutland Gizz
3 Replies
Login or Register to Ask a Question