Linux shell no echo

How to Turn Off Echo While Executing a Shell Script Linux

How to turn off echo while executing a shell script Linux

You could use Bash redirection :

command > /. /path_to_file is a shortcut of the previous command.

To do both at the same time to the same output: command >/. /path_to_file 2>&1 .

2>&1 means redirect 2 (stderr) to 1 (stdout which became path_to_file).
You could replace path_to_file by /dev/null if you don’t want to retrieve the output of your command.

Otherwise, you could also store the output of a command :

$ var=$(command) # Recent shell like Bash or KSH
$ var=`command` # POSIX compliant

In this example, the output of command will be stored in $var .

How do I turn off echo in a terminal?

stty_orig=`stty -g`
stty -echo
echo 'hidden section'
stty $stty_orig

How to echo shell commands as they are executed

set -x or set -o xtrace expands variables and prints a little + sign before the line.

set -v or set -o verbose does not expand the variables before printing.

Use set +x and set +v to turn off the above settings.

On the first line of the script, one can put #!/bin/sh -x (or -v ) to have the same effect as set -x (or -v ) later in the script.

The above also works with /bin/sh .

See the bash-hackers’ wiki on set attributes, and on debugging.

$ cat shl
#!/bin/bash

DIR=/tmp/so
ls $DIR

$ bash -x shl
+ DIR=/tmp/so
+ ls /tmp/so
$

How to hide command output in Bash

Use this.

 /your/first/command 
/your/second/command
> &> /dev/null

Explanation

To eliminate output from commands, you have two options:

    Close the output descriptor file, which keeps it from accepting any more input. That looks like this:

your_command "Is anybody listening?" >&-

Usually, output goes either to file descriptor 1 (stdout) or 2 (stderr). If you close a file descriptor, you’ll have to do so for every numbered descriptor, as &> (below) is a special BASH syntax incompatible with >&- :

your_command "Hello?" > /dev/null

For output redirection to a file, you can direct both stdout and stderr to the same place very concisely, but only in bash:

/your/first/command &> /dev/null

Finally, to do the same for a number of commands at once, surround the whole thing in curly braces. Bash treats this as a group of commands, aggregating the output file descriptors so you can redirect all at once. If you’re familiar instead with subshells using ( command1; command2; ) syntax, you’ll find the braces behave almost exactly the same way, except that unless you involve them in a pipe the braces will not create a subshell and thus will allow you to set variables inside.

 /your/first/command 
/your/second/command
> &> /dev/null

See the bash manual on redirections for more details, options, and syntax.

Читайте также:  Linux run scripts on boot

How to keep the internal shell script running while the main shell script ends?

You can put the process in the background with & :

#!/bin/bash

if pgrep infiloop > /dev/null ;
then
echo «Process is running.»
else
exec /u/team/infiloop.sh > /u/team/infiloopOutput.txt &
echo «Process was not running, started process $!»
fi

How can I suppress all output from a command using Bash?

The following sends standard output to the null device (bit bucket).

And if you also want error messages to be sent there, use one of (the first may not work in all shells):

scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null

And, if you want to record the messages, but not see them, replace /dev/null with an actual file, such as:

For completeness, under Windows cmd.exe (where «nul» is the equivalent of «/dev/null»), it is:

Date is getting Printed in echo command while executing shell script

First of all, you should prefer $() in bash to fork a subshell.

To print out your desired command without being interpreted by bash , you need to escape $ , i.e.

#!/bin/bash
echo "0 20 * * * touch /global/appaem/aem/wrap-lock/wrap.lock-\$(date +\"%Y%m%d\")" >> /tmp/tmp.txt

Источник

How do I turn off echo in a terminal?

Obviously, I don’t want the password being echoed to the terminal, so I want to turn off echo for the duration of the read. I know there’s way to do this with stty , but I’ll ask the question for the benefit of the community whilst I go read the manpage. 😉

4 Answers 4

stty_orig=`stty -g` stty -echo echo 'hidden section' stty $stty_orig 

This is certainly the way I’ve done it before, if memory serves. Just out of curiosity, is this any different from stty -echo; read foo; stty echo ?

More general, better than Wikipedia when you’re new to unix @AbhishekGupta: 1. learn how to scroll, search, and quit less (or more ). 2. man whateveryouwonderabout or, for bash builtin commands such as «set», help set . Good luck on your journey 😉

read -s password works on my linux box.

This is useful information but -s seems to be a Bash extension, as read -s foo produces read: 1: Illegal option -s in Dash (the Bourne shell which Ubuntu symlinks to /bin/sh ).

You can use ‘-s’ option of read command to hide user input.

echo -n "Password:" read -s password if [ $password != ". " ] then exit 1; # exit as password mismatched # fi 

Also you can use ‘stty -echo’ if you want to hide from terminal to print. And restore the terminal settings using «stty echo»

But I think for getting password input from user ‘read -s password’ is more than enough.

Читайте также:  Top packages for linux

Bourne Shell Script :

#!/bin/sh # Prompt user for Password echo -n 'Password: ' # Do not show what is being typed in console by user stty -echo # Get input from user and assign input to variable password read password # Show what is being typed in console stty echo 

stty manual command for more information:

stty manual snippets:

 STTY(1) stty 5.2.1 (March 2004) STTY(1) NAME stty - change and print terminal line settings SYNOPSIS stty [-F DEVICE] [--file=DEVICE] [SETTING]. stty [-F DEVICE] [--file=DEVICE] [-a|--all] stty [-F DEVICE] [--file=DEVICE] [-g|--save] DESCRIPTION Print or change terminal characteristics. -a, --all print all current settings in human-readable form -g, --save print all current settings in a stty-readable form -F, --file=DEVICE open and use the specified DEVICE instead of stdin --help display this help and exit --version output version information and exit Optional - before SETTING indicates negation. An * marks non-POSIX settings. The underlying system defines which settings are available. Local settings: [-]echo echo input characters 

Источник

Shell Script — No echo and no errors

I’m not sure why i’m not getting any error or any echoed information from this script. It’s one of those exercises where we are supposed to correct mistakes in the script. I just need a nudge to point me in the right direction.

#!/bin/bash clear echo while [ "$CHOICE" = "y" ] do # Rocket ship count down code for num in do echo "$num . " sleep 1 done echo echo -n 'We have main engine start.' # Rocket ship powering up for num in do echo -n ". " sleep 1 done # End rocket ship powering up loop echo -e '\e[1;31mlift off!\e[0m' echo # Loop to build rocket ship ################################################## Do not alter the code between these hash marks ################################################### for rocket_ship_part in '\e[1;30m A \e[0m' '\e[1;30m / \ \e[0m' '\e[1;30m |=| \e[1;30m' '\e[1;30m |\e[1;31mU\e[0m\e[1;30m|' \ '\e[1;30m |\e[1;34mS\e[0m\e[1;30m|' '\e[1;30m |\e[1;32mA\e[0m\e[1;30m|' '\e[1;30m _|=|_ ' '\e[1;30m / | | \ ' '\e[1;30m | \|/ | ' \ '\e[1;30m |/"\e[1;31m"\e[0m"\| ' '\e[1;31m """ \e[0m' '\e[1;31m """ \e[0m' '\e[1;31m " \e[0m' '\e[1;31m " \e[0m' '\e[1;31m . \e[0m' \ '\e[1;31m . \e[0m' '\e[1;31m . \e[0m' '\e[0;31m . \e[0m' '\e[0;31m . \e[0m' '\e[0;30m . \e[0m' '\e[0;30m . \e[0m' ################################################## Do not alter the code between these hash marks ################################################### do echo -e "$rocket_ship_part" sleep 1 done # Up, up and away! for altitude in do echo sleep 0.5 done clear echo 'To the stars and beyond!' echo echo -n "Do ypu want to launch the rocket again? (y/n) " read CHOICE clear done 

Источник

Can I have the same output with print command. I actually went through their manuals, where $ man print describes it as Run-mailcap-programs which term I never heard before. And came to know that it is used to find the correct program to open a file with, based on MIME.

So is there any way to print a simple line using print alone instead of echo ?

Found similar kind of problem at Need to assign the contents of a text file to a variable in a bash script, but people suggesting echo over print there. I tried following, but got issues.

$ print --"text/plain" "prayag works on JVM" Warning: unknown mime-type for "prayag works on JVM" -- using "application/octet-stream" Error: no such file "prayag works on JVM" 
$ print --"text/plain" application.properties Warning: unknown mime-type for "application.properties" -- using "application/octet-stream" Error: no "print" mailcap rules found for type "application/octet-stream" 
$ awk '' application.properties prayag works on JVM 

3 Answers 3

You’re confusing many things here, let me try and tease this apart for you.

  1. awk » . This has nothing to do with the print you’re asking about. awk is a dynamic programming language which contains its own commands, of which print is one of them.
  2. print —«text/plain» «prayag works on JVM» and other forms The command print is for displaying files per the contents of the mailcap file, using the designated «handler» for each particular file type, not for printing strings to the screen. excerpt from the print man page

run-mailcap, view, see, edit, compose, print — execute programs via entries in the mailcap file run-mailcap (or any of its aliases) will use the given action to process each mime-type/file in turn. Each file is spec‐ ified as its mime-type, its encoding (e.g. compression), and filename together, separated by colons. If the mime-type is omitted, an attempt to determine the type is made by trying to match the file’s extension with those in the mime.types files. If the encoding is omitted, it will also be determined from the file’s extensions. Currently supported encodings are gzip (.gz), bzip (.bz), bzip2 (.bz2), and compress (.Z). A filename of «-» can be used to mean «standard input», but then a mime-type must be specified.

$ type -a echo echo is a shell builtin echo is /bin/echo 

Example

zsh % print "hello world" hello world 

printf

Perhaps you’re looking for the command printf ?

$ printf "%s\n" "hello stringy world" hello stringy world $ printf "this is some padded digits: %04d\n" "10" this is some padded digits: 0010 

The command printf takes a formatting set of special characters so that you can instruct it how you want to display things such as strings or digits etc. See the man page for printf .

mailcap

So a lot of people are probably oblivious as to the file /etc/mailcap . This file contains mime-types (headers from files which identifies what type of file a file is). You can then create entries in this mailcap file which designate what tool to use to open a particular mime-type for a file.

Example

text/html; /usr/bin/sensible-browser '%s'; description=HTML Text; nametemplate=%s.html application/x-troff-man; /usr/bin/nroff -mandoc -Tutf8; copiousoutput; print=/usr/bin/nroff -mandoc -Tutf8 | print text/plain:- application/x-ogg; /usr/bin/mplayer '%s'; description="OggVorbis Audio" application/ogg; /usr/bin/mplayer '%s'; description="OggVorbis Audio" audio/mpeg; /usr/bin/mplayer '%s'; description="MPEG Audio Format" audio/x-mpegurl; /usr/bin/mplayer '%s'; description="Icecast Playlists" audio/x-ms-wax; /usr/bin/mplayer '%s'; description="Audio Format" 

The above lines say that if you get a file with the mime-type audio/mpeg , then use the tool /usr/bin/mplayer to open this file.

These rules are generally used by the email tool, but other tools can take advantage of these entries within the mailcap file as well.

If you’re interested in learning more about mime-types or mailcap see the following references.

Источник

Оцените статью
Adblock
detector