How to turn off echo while executing a shell script Linux [duplicate] - linux

This question already has answers here:
How to silence output in a Bash script?
(9 answers)
Closed 6 years ago.
Here is a simple thing i was working on
echo "please enter a command"
read x
$x
checkexitstatus()
{...}
checkexit status is a ifloop created somewhere else just to check exit status
What i want to know is
Is there any way that when i run the $x that it wont be displayed on the screen
I want to know if it is possible without redirecting the output to a file

No, it isn't possible.
$x &> /dev/null

You could use Bash redirection :
command 1> /.../path_to_file => to redirect stdout into path_to_file.
command > /.../path_to_file is a shortcut of the previous command.
command 2> /.../path_to_file => to redirect stderr into path_to_file
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.

If you want to turn off only the echo command and not other commands that send their output to the stdout, as the title suggests, you can possibly (it may break the code) create an alias for echo
alias echo=':'
now echo is an alias for noop. You can unalias it by unalias echo.

Related

Redirect both standard output and standard error to different file in the same command [duplicate]

I know this much:
$ command 2>> error
$ command 1>> output
Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?
Just add them in one line command 2>> error 1>> output
However, note that >> is for appending if the file already has data. Whereas, > will overwrite any existing data in the file.
So, command 2> error 1> output if you do not want to append.
Just for completion's sake, you can write 1> as just > since the default file descriptor is the output. so 1> and > is the same thing.
So, command 2> error 1> output becomes, command 2> error > output
Try this:
your_command 2>stderr.log 1>stdout.log
More information
The numerals 0 through 9 are file descriptors in bash.
0 stands for standard input, 1 stands for standard output, 2 stands for standard error. 3 through 9 are spare for any other temporary usage.
Any file descriptor can be redirected to a file or to another file descriptor using the operator >. You can instead use the operator >> to appends to a file instead of creating an empty one.
Usage:
file_descriptor > filename
file_descriptor > &file_descriptor
Please refer to Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.
Like that:
$ command >>output 2>>error
Or if you like to mix outputs (stdout & stderr) in one single file you may want to use:
command > merged-output.txt 2>&1
Multiple commands' output can be redirected. This works for either the command line or most usefully in a bash script. The -s directs the password prompt to the screen.
Hereblock cmds stdout/stderr are sent to seperate files and nothing to display.
sudo -s -u username <<'EOF' 2>err 1>out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to a single file and display.
sudo -s -u username <<'EOF' 2>&1 | tee out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to separate files and stdout to display.
sudo -s -u username <<'EOF' 2>err | tee out
ls; pwd;
EOF
Depending on who you are(whoami) and username a password may or may not be required.

How should I redirect stderr on commands that with arguement?

I have a shell script I want to do:
./xxx.sh -a 1 -b 2 -c 3 2>/dev/null
However, the 2 is treated as argument
Similerly with the following command
echo test 2>aaa.txt
my intention is to direct strerr to aaa.txt, however, I got:
cat aaa.txt
test 2
as you can see 2 is also treated as an argument. How should I redirect stderr on commands that with argument?
I think you are using csh or tcsh
because 2> redirect does not work with csh or tcsh.
Use the chsh command to change your shell to /bin/sh or /usr/local/bin/bash in order to use the 2> style redirect. Note: Do not change root's shell to /usr/local/bin/bash
csh and tcsh cannot redirect standard out and error separately, but >& will redirect the combined output to a file.
and then you can try to use the same command or with "()"
like ( your command ) 2> /dev/null
or you can also do it with 2 shells.
Example: csh -c 'SOME_COMMAND 1>/dev/null' |& tee file.txt

How to redirect STDOUT and STDERR in a file for every command? [duplicate]

This question already has answers here:
How to redirect output of an entire shell script within the script itself?
(6 answers)
Closed 3 years ago.
I'm trying to store both STDOUT and STDERR from the terminal (and if possible STDIN given by user) in a file for every command.
So i started creating a trap function to execute every command in a edited manner like:
shopt -s extdebug
preexec_invoke_exec () {
[ -n "$COMP_LINE" ] && return # do nothing if completing
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
eval `history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"` |& tee ~/recent_output.txt
return 1 # This prevent executing of original command
}
trap 'preexec_invoke_exec' DEBUG
and saving the above file and executing
source file.sh
This did the work what i wanted but stopped some commands from executing like
cd ..
The reason for this was piping creates a sub-shell and then executes every command in it. So the main shell remains unaffected.
Even the script functionality of bash i.e
script ~/recent_output.txt
worked but only gives output after you do exit in in terminal
So, basically i want to store/get the output of previous command executed in the bash terminal. You can help me with any language (golang,python...).
It is possible to capture commands, stderr and stdout of a bash script (say x.sh), using:
bash -x x.sh 2> >(tee err.txt) | tee out.txt
The err.txt will capture the executed commands (prefixed with '+'), and the stderr of each command. The out.txt will capture the output

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

How do I redirect the output from a bash script to stderr

I've written a bash script. It has a few echo statements. It invokes a few 3rd party tools. I would like to redirect everything that goes to stdout (from my echo statements and all the tools output) to stderr. How do I do that?
You need to redirect the stdout of the command to stderr.
your_command.sh 1>&2
If you want to do this from within the script, you can wrap your whole script into one function and redirect its output to stderr:
main() {
echo hello
echo world
some_script.sh
}
main 1>&2
exec >&2
Put that at the top of your script to redirect all future output to stderr.
$ help exec
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The solution that worked for me was to enclose the text of the script within ()'s and redirect stdout to stderr like so:
(
echo 1
echo 2
tool1
) 1>&2

Resources