Suppress Error message while using cat command - linux

I have a script where I recursively copy the contents of the files in a folder using cat command as follows
test -f /tmp/$F || cat $F > /tmp/$F
I get the following error
cat: read error: Invalid argument
I want to know how can I suppress this error. I only have access to shell interpreter (no bash).
Thanks

Send error messages to /dev/null.
cat $F 1>/tmp/$F 2>/dev/null

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 to redirect standard error to a file

In linux if I want to redirect standard error to a file, I can do this:
$ls -l /bin/usr 2> ls-error.txt
But when I try:
$foo=
$echo ${foo:?"parameter is empty"} 2> ls-error.txt
The result in terminal is:
bash: foo: parameter is empty
It doesn't work!
Can somebody explain why?
I thought ${parameter:?word} would send the value of word to standard error.
echo ${foo:?"parameter is empty"} 2>ls-error.txt redirects the stderr of echo, but the error message is produced by the shell while expanding
${foo:?"parameter is empty"}.
You can get the result you want by redirecting a block (or a subshell) instead so that the shell's stderr is included in the redirection:
{ echo "${foo:?"parameter is empty"}"; } 2>ls-error.txt
Try this command:
($echo ${foo:?"parameter is empty"}) 2> ls-error.txt
In case you would like to redirect both sandard and error output, AND to still get these messages when executing your command, you can use the tee command:
$echo ${foo:?"parameter is empty"} |& tee -a ls-error.txt

Difference between shell command `ls –z >output.txt` and `ls –z 2>output.txt`?

I'm trying to learn shell commands. I know ls >output.txt saves the output to output.txt. However, what exactly does ls -z >output.txt do? In my book, it says it does Not save the output to output.txt. If this is true, where Does it save / print it? Also, is -z what causes it to not save it?
Lastly, what does ls -z 2>output.txt do? I know 2 refers to stderr (so the standard error). Does this mean it saves the error (if any) of ls in output.txt? If yes, where does the stdout get printed / saved? And what does the -z mean in this case?
Thanks in advance!
There is no option -z for ls on Linux. So let's see what happens:
$ LANG=C ls -z > /tmp/x
ls: invalid option -- 'z'
Try 'ls --help' for more information.
The error message goes to STDERR which is connected to the terminal.
The standard output (which is empty) is redirected to /tmp/x so we get an empty file.
$ LANG=C ls -z 2> /tmp/x
In this second scenario STDOUT is connected to the terminal, however there is no output. The error message which got sent to STDERR lands in /tmp/x:
$ cat /tmp/x
ls: invalid option -- 'z'
Try 'ls --help' for more information.

How can I suppress error messages of a command?

How can I suppress error messages for a shell command?
For example, if there are only jpg files in a directory, running ls *.zip gives an error message:
$ ls *.zip
ls: cannot access '*.zip': No such file or directory
Is there an option to suppress such error messages? I want to use this command in a Bash script, but I want to hide all errors.
Most Unix commands, including ls, will write regular output to standard output and error messages to standard error, so you can use Bash redirection to throw away the error messages while leaving the regular output in place:
ls *.zip 2> /dev/null
$ ls *.zip 2>/dev/null
will redirect any error messages on stderr to /dev/null (i.e. you won't see them)
Note the return value (given by $?) will still reflect that an error occurred.
To suppress error messages and also return the exit status zero, append || true. For example:
$ ls *.zip && echo hello
ls: cannot access *.zip: No such file or directory
$ ls *.zip 2>/dev/null && echo hello
$ ls *.zip 2>/dev/null || true && echo hello
hello
$ touch x.zip
$ ls *.zip 2>/dev/null || true && echo hello
x.zip
hello
I attempted ls -R [existing file] and got an immediate error.
ls: cannot access 'existing file': No such file or directory
So, I used the following:
ls -R 2>dev/null | grep -i [existing file]*
ls -R 2>dev/null | grep -i text*
Or, in your case:
ls -R 2>dev/null | grep -i *.zip
My solution with a raspberry pi3 with buster.
ls -R 2>/dev/null | grep -i [existing file]*
2>/dev/null is very usefull with Bash script to avoid useless warnings or errors.
Do not forget slash caracter

if command "cat /dev/net/tun" result $string then

I'm creating a script which check if a VPS do have TUN driver enabled.
The check command is :
cat /dev/net/tun
if it return:
cat: /dev/net/tun: File descriptor in bad state
the module is enabled. otherwise return ERROR.
Here is my script:
tunstring="File descriptor in bad state"
if cat /dev/net/tun | grep -q "$tunstring"; then
echo "GOOOOOD"
else
echo "ERROR"
fi
I get ERROR message.
I tried the same script with a text file and it worked...
Since that output is being written on stderr you can use:
tunstring="File descriptor in bad state"
if cat /dev/net/tun |& grep -q "$tunstring"; then
echo "GOOOOOD"
else
echo "ERROR"
fi
|& pipes previous command's stdout and stderr to next in pipe line.
Looks like your VPS path i.e /dev/net/tun isn't valid anymore and cat command is failing to read it.

Resources