LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-08-2010, 09:21 AM   #1
replica88
Member
 
Registered: Nov 2009
Posts: 48

Rep: Reputation: 16
What is the @Echo Off alternative for a shell script?


I want to suppress the output resulting from the commands in my scripts, when writing batch commands in dos I would just use @Echo Off.

I have written an example of what I want to achieve
Code:
#!/bin/bash
clear
@Echo off

echo updating...
apt-get update
echo done
I would like the output to simply read
Code:
root@Fileserver:/# ./testscript.txt
updating...
done
but instead I get

Code:
root@Fileserver:/# ./testscript.txt
./testscript.txt: line 3: @Echo: command not found
updating...
Ign file:  Release.gpg
Ign file:  Translation-en_GB
Ign file:  Release
Ign file:  Packages
Hit http://security.ubuntu.com hardy-security Release.gpg
Ign http://security.ubuntu.com hardy-security/main Translation-en_GB
Ign http://security.ubuntu.com hardy-security/restricted Translation-en_GB
Hit http://gb.archive.ubuntu.com hardy Release.gpg
Hit http://gb.archive.ubuntu.com hardy/main Translation-en_GB
Hit http://gb.archive.ubuntu.com hardy/restricted Translation-en_GB
Ign http://security.ubuntu.com hardy-security/universe Translation-en_GB
Ign http://security.ubuntu.com hardy-security/multiverse Translation-en_GB
Hit http://security.ubuntu.com hardy-security Release
Hit http://gb.archive.ubuntu.com hardy/universe Translation-en_GB
Hit http://gb.archive.ubuntu.com hardy/multiverse Translation-en_GB
Hit http://gb.archive.ubuntu.com hardy-updates Release.gpg
Ign http://gb.archive.ubuntu.com hardy-updates/main Translation-en_GB
Ign http://gb.archive.ubuntu.com hardy-updates/restricted Translation-en_GB
Ign http://gb.archive.ubuntu.com hardy-updates/universe Translation-en_GB
Ign http://gb.archive.ubuntu.com hardy-updates/multiverse Translation-en_GB
Hit http://gb.archive.ubuntu.com hardy Release
Hit http://gb.archive.ubuntu.com hardy-updates Release
Hit http://security.ubuntu.com hardy-security/main Packages
Hit http://gb.archive.ubuntu.com hardy/main Packages
Hit http://security.ubuntu.com hardy-security/restricted Packages
Hit http://security.ubuntu.com hardy-security/main Sources
Hit http://security.ubuntu.com hardy-security/restricted Sources
Hit http://security.ubuntu.com hardy-security/universe Packages
Hit http://security.ubuntu.com hardy-security/universe Sources
Hit http://security.ubuntu.com hardy-security/multiverse Packages
Hit http://security.ubuntu.com hardy-security/multiverse Sources
Hit http://gb.archive.ubuntu.com hardy/restricted Packages
Hit http://gb.archive.ubuntu.com hardy/main Sources
Hit http://gb.archive.ubuntu.com hardy/restricted Sources
Hit http://gb.archive.ubuntu.com hardy/universe Packages
Hit http://gb.archive.ubuntu.com hardy/universe Sources
Hit http://gb.archive.ubuntu.com hardy/multiverse Packages
Hit http://gb.archive.ubuntu.com hardy/multiverse Sources
Hit http://gb.archive.ubuntu.com hardy-updates/main Packages
Hit http://gb.archive.ubuntu.com hardy-updates/restricted Packages
Hit http://gb.archive.ubuntu.com hardy-updates/main Sources
Hit http://gb.archive.ubuntu.com hardy-updates/restricted Sources
Hit http://gb.archive.ubuntu.com hardy-updates/universe Packages
Hit http://gb.archive.ubuntu.com hardy-updates/universe Sources
Hit http://gb.archive.ubuntu.com hardy-updates/multiverse Packages
Hit http://gb.archive.ubuntu.com hardy-updates/multiverse Sources
Reading package lists... Done
done
any help would be appreciated,

Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-08-2010, 09:31 AM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
When you call the script, just redirect the standard output like this
scriptname > /dev/null
Error messages will still be printed.

Search on bash redirect standard output for more information.
 
Old 01-08-2010, 09:39 AM   #3
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 113

Rep: Reputation: 23
You need to redirect the stdout and stderr of the apt-get update to some where else besides the screen.
IF you really don't want (or need) to ever see any output. Then you can redirect to /dev/null.

Example:
Code:
apt-get update 2>&1 >/dev/null
--ET
 
Old 01-08-2010, 09:46 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can do it from inside the script itself, using exec. For example:
Code:
#!/bin/bash
exec > /dev/null 2>&1
will redirect both the standard output and the standard error to /dev/null and every subsequent command will be affected. If you still want to preserve the output of some commands, as in your example, better to explicitly redirect the standard output of the commands you want to hide, e.g.
Code:
#!/bin/bash
echo updating...
apt-get update > /dev/null
echo done
An aside note: if I remember well in DOS the @ECHO OFF statement, does not hide the output of the commands (correct me if I'm wrong), but the print out of the command itself. In unix shells this is not the default behaviour, but you can trigger it using the -x option:
Code:
#!/bin/bash -x
this will show you every command that is going to be executed, useful for debugging purposes.

Edit: a bit too late...

Last edited by colucix; 01-08-2010 at 09:48 AM.
 
Old 01-08-2010, 09:47 AM   #5
replica88
Member
 
Registered: Nov 2009
Posts: 48

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by slacker_et View Post
You need to redirect the stdout and stderr of the apt-get update to some where else besides the screen.
IF you really don't want (or need) to ever see any output. Then you can redirect to /dev/null.

Example:
Code:
apt-get update 2>&1 >/dev/null
--ET
cool, so if I wanted to save the output to a txt file I assume this would work?

Code:
apt-get update 2>&1 >/aptupdatelog.txt
Thanks
 
Old 01-08-2010, 09:52 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by slacker_et View Post
You need to redirect the stdout and stderr of the apt-get update to some where else besides the screen.
IF you really don't want (or need) to ever see any output. Then you can redirect to /dev/null.

Example:
Code:
apt-get update 2>&1 >/dev/null
--ET
Just a note here: the order of redirections is important. This statement does not redirect standard error to /dev/null, since stderr is redirected to stdout when this one still points to the terminal. The correct way is:
Code:
command > /dev/null 2>&1
Just for sake of the original poster...
 
2 members found this post helpful.
Old 01-08-2010, 09:59 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by replica88 View Post
cool, so if I wanted to save the output to a txt file I assume this would work?
replica88, yes... /dev/null is a kind of black hole in unix systems, but you can redirect to a file as well. Have a look at bash documentation, as previously suggested. Here is the I/O redirection chapter of the Advanced Bash Scripting Guide.. a bit difficult to read for beginners, but useful to take as a reference. Other documentation is available on www.tldp.org.
 
Old 01-08-2010, 10:09 AM   #8
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 113

Rep: Reputation: 23
Quote:
Originally Posted by colucix View Post
Just a note here: the order of redirections is important. This statement does not redirect standard error to /dev/null, since stderr is redirected to stdout when this one still points to the terminal. The correct way is:
Code:
command > /dev/null 2>&1
Just for sake of the original poster...
Ooops ! You correct.
It's still early in the morning for me.

--ET
 
1 members found this post helpful.
Old 01-08-2010, 10:15 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by slacker_et View Post
Ooops ! You correct.
It's still early in the morning for me.

--ET
He he... I've just had a cup of coffee after launch, instead.
 
Old 10-01-2012, 07:54 AM   #10
EODSteven
Member
 
Registered: Oct 2012
Location: Manchester, Tn
Distribution: Ubuntu 14.04, Windows 8, Windows Server 2012. Ubuntu Server 14.04
Posts: 196

Rep: Reputation: 3
Thumbs up Suppressing output of script to terminal

Quote:
Originally Posted by colucix View Post
You can do it from inside the script itself, using exec. For example:
Code:
#!/bin/bash
exec > /dev/null 2>&1
will redirect both the standard output and the standard error to /dev/null and every subsequent command will be affected. If you still want to preserve the output of some commands, as in your example, better to explicitly redirect the standard output of the commands you want to hide, e.g.
Placing this at the top of the Script suppresses all output to the terminal Thank you! Sorry about posting on such an old thread but it took me awhile to find this good solution and I hope I can help others as well!

Last edited by EODSteven; 10-01-2012 at 07:57 AM. Reason: added reason for late post
 
Old 10-01-2012, 01:09 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes, it's not usually advised to open up an old thread unless you have something substantive to add to that discussion. But it's nice to hear that it helped you.

By the way, also check the options for the commands you use. Very often they include some way to disable output. The most common flags for this are probably -q (quiet) and -s (silent).

In this case "apt-get update -q=2" appears to turn off the default output.

Last edited by David the H.; 10-01-2012 at 01:12 PM.
 
  


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
How do you echo the folder name your currently in (using SHELL)? Techno Guy Linux - Newbie 8 03-09-2009 03:34 AM
how to use single quote in bash shell like: echo ''\''' linuxtyh Linux - General 6 12-12-2008 12:56 AM
Bash shell file... echo on? carlos_vcan Linux - Newbie 3 06-12-2008 11:10 PM
crontabbed shell script, trying to echo/cat something zaubara Programming 2 06-13-2004 08:18 PM


All times are GMT -5. The time now is 10:34 AM.

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