Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By Subsonics

Simple shell script question.

This is a discussion on Simple shell script question. within the Linux Programming forums, part of the Platform Specific Boards category; I have to write a shell script that uncompresses files based on their suffix. i.e if the file is tar ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Simple shell script question.

    I have to write a shell script that uncompresses files based on their suffix. i.e if the file is tar I would use tar -x

    .. Simple enough.

    So here is my script:
    Code:
    #!/bin/sh
            case $1 in
                    *.tar*) `tar -xzf $1` ;;
                    *.zip*) `unzip $1` ;;
                    *.z|*.gz) `gunzip $1` ;;
            esac
    
    [avanish@pc25 ~]$ ./third.sh testbinary.zip
    ./third.sh: line 7: Archive:: command not found

    The file uncompresses, because I check it with an ls afterword, but why do I keep getting that error??

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I'm just learning this stuff myself, but do you need the backticks? Try it without
    Code:
    #!/bin/sh
            case $1 in
                    *.tar*) tar -xzf $1 ;;
                    *.zip*) unzip $1 ;;
                    *.z|*.gz) gunzip $1 ;;
            esac
    MK27 likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    How is this relevant to C programming?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe the tar file contains a C file.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by memcpy View Post
    How is this relevant to C programming?
    Yep! this should be moved to "Linux Programming" or posted in some Linux scripting forum.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Yes. The compressed file is a C file . I also felt that someone on here would be more likely to help me out. Ooga booga, you are truly a saint! Thank you.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    What do the backticks do then? I was under the impression that you placed them around every command in the script.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Backticks are used for command substitution, it will replace the command with the result of the command. It's useful if you for example want to assign the output to a variable, you may use $() instead which accomplishes the same thing. It's not necessary to use this in a shell script per se, unless you need command substitution.
    MK27 likes this.

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    FYI,
    there is a linux tool that does this called 'atool':
    `aunpack myzipfile.tar.gz`
    `aunpack wikipedia.zip`

    `sudo apt-get install atool`
    Last edited by Kleid-0; 04-19-2012 at 03:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple shell question and more if needed
    By TaiL in forum C Programming
    Replies: 2
    Last Post: 09-24-2009, 04:29 PM
  2. shell script basic question
    By PedroTuga in forum Linux Programming
    Replies: 1
    Last Post: 09-09-2006, 04:24 AM
  3. Shell script
    By diamond in forum Linux Programming
    Replies: 5
    Last Post: 03-26-2004, 08:39 AM
  4. shell script
    By duffy in forum Tech Board
    Replies: 1
    Last Post: 10-24-2002, 06:15 PM
  5. Really need help for this C shell script
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-22-2002, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21