LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2014, 10:44 AM   #1
lapishater276
Member
 
Registered: Jun 2014
Location: Narnia
Distribution: Crunchbang, Arch Linux ARM, Xubuntu
Posts: 43

Rep: Reputation: 0
dampering messages in shell scripting


I am writing a shell script that can decompress archives in .tar.gz and .zip format. I have already made them into usable commands executable from the bash shell (script placed in /usr/local/bin folder). The last step I need to complete is to be able to damper or turn off messages from the utilities the scripts use. I do not want the commands I am using in the script (tar, gzip, zip) to output messages. How would i do this?

current script:
#!/bin/bash
if [ "$1" = "--help" ] ; then
echo " Usage: To decompress archives in the .zip or .tar.gz format

2 required argument:
Argument 1 = Name of archive without file extension
Argument 2 = File extension of archive. -t for tar.gz and -z for zip

Warnings:
1. Do not add the file extension to argument 1. The script does not
require the file extension to work.
2. you must always have the file extension before the keep/delete option.
You will confuse the script if you do otherwise

Examples:
delete and decompress a .tar.gz archive:
decompressor example-archive -td
delete and unzip a .zip archive:
decompressor example-archive -zd:
keep and decompress a .tar.gz archive:
decompressor example-archive -tk
keep and unzip a .zip archive:
decompressor example-archive -zk

--lapishater276"
exit 0
fi
if [ "$2" = "-td" ] ; then
echo "decompressing and deleting $1..."
gunzip $1.tar.gz
tar -xvf $1.tar
rm $1.tar
echo "$1 decompressed and deleted."
elif [ "$2" = "-zd" ] ; then
echo "unzipping and deleting $1..."
unzip $1.zip
rm $1.zip
echo "$1 unzipped and deleted."
elif [ "$2" = "-tk" ] ; then
echo "decompressing and saving $1..."
gunzip $1.tar.gz
tar -xvf $1.tar
gzip $1.tar
echo "$1 decompressed and saved"
elif [ "$2" = "-zk" ] ; then
echo "unzipping and saving $1..."
unzip $1.zip
echo "$1 unzipped and saved"
else
echo "do 'decompressor --help' to see how to use this command"
fi
 
Old 07-04-2014, 11:27 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
You redirect stdout and/or stderr to /dev/null:
Code:
tar -xvf file.tar >/dev/null
(In that particular case it would make more sense to remove the "v" parameter, as that's what's causing tar to print the name of every file in the archive.)
 
Old 07-04-2014, 11:41 AM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
To send both stdout and stderr to /dev/null:
Code:
tar -xvf file.tar >/dev/null 2>&1
However, this is a bad idea, especially since you are not checking the return value of these utilities. I recommend at least using an if statement to check if the functions fail or not.
 
Old 07-04-2014, 01:04 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,961

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
FYI if you do not specify the -v (verbose) option tar will not display any file names etc. unzip has a quiet option -q that when invoked will not display file names too. However, you will still see error messages.

The redirect will prevent all messages from being displayed but then you will not see specific error messages if you enter the wrong file or archive type. As stated checking for return status would a good idea.

Also the -z option will untar gz files so you do not need two commands.
 
1 members found this post helpful.
Old 07-04-2014, 04:09 PM   #5
lapishater276
Member
 
Registered: Jun 2014
Location: Narnia
Distribution: Crunchbang, Arch Linux ARM, Xubuntu
Posts: 43

Original Poster
Rep: Reputation: 0
How would i add an if statement to see whether or not the script failed?
 
Old 07-04-2014, 04:41 PM   #6
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
Quote:
Originally Posted by lapishater276 View Post
How would i add an if statement to see whether or not the script failed?
You mean, how do you add an if statement to see if one of the commands used by you script, failed?

Just about every command in existence follows the convention of generating a non-zero exit code if the operation somehow fails. Different commands may return different values depending on the nature of the error, but it's fairly safe to assume that a non-zero value means something went wrong.

The exit code is stored in the $? variable, so a simple test would look something like this:
Code:
if [ "$?" != "0" ]; then
   echo Oops, something went wrong.
   exit 1
fi
Here I simply test for a non-zero value and print a simple error message before exiting the script with an exit code of 1.

Note: Every command returns an exit code, including the if command. If you need to run multiple tests on an exit code for a specific command to deal with different error conditions, you'll have to store the contents of $? in a different variable first. Otherwise, the first test will clear the contents of $?, and all subsequent tests will fail.

Last edited by Ser Olmy; 07-04-2014 at 05:28 PM. Reason: typo
 
Old 07-04-2014, 05:39 PM   #7
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
Some more ways to respond to errors:

1) Use the command as a parameter to the if statement:
Code:
if tar -xvf file.tar >/dev/null 2>&1
then
echo "success"
else
echo "tar failed"
fi
2) Write an error function and use the or statement:
Code:
error()
{
echo "$1"
exit 1
}

tar -xvf file.tar >/dev/null 2>&1 || error "tar failed"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
win32,shell code,shell programming,shell scripting? mr.cracker Linux - Newbie 4 07-13-2013 12:20 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-27-2008 12:50 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM


All times are GMT -5. The time now is 07:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration