I have following small shell script.
value='testdir/imp'
`mkdir -m 755 $value`
echo $
the out put is
$ ./dir.sh
mkdir: Failed to make directory "testdir/imp"; Permission denied
2
Here i want to print this error message to a text file
How i can do this
In your case
$ ./dir.sh 2 > log.file
should put you error message in the file log.file
mkdir prints error messages to stderr. You can also redirect the output from stderr to a file. Knowing these 2 things you can do this:
mkdir /etc/test 2>somefile
And then the output from mkdir will be in a somefile.
The syntax 2>somefile redirects stderr to a file called somefile.
Related
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
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
I wrote a bash script rm_remote_file.sh that ssh to a set of remote machines and delete a file. I used & at the end of each function call to run these command in parallel, the script looks like this:
#!/bin/bash
rm_remote_file() {
echo "Removing file on node $1:"
ssh $1 'rm ~/test_file'
}
for node in node1.com node2.com node3.com; do
rm_remote_file $node &
done
When test_file exists on each node - the rm command succeeds - the output of this script is:
Removing file on node node1.com:
Removing file on node node2.com:
Removing file on node node3.com:
I prefer having each hostname printed out. However, if the test_file does not exist on each node - the rm command fails - the output of this script is:
rm: cannot remove ‘~/test_file’: No such file or directory
rm: cannot remove ‘~/test_file’: No such file or directory
rm: cannot remove ‘~/test_file’: No such file or directory
So the printing of the nodes' host names are suppressed by this error message. I think this behavior has something to do with the I/O redirection and using things such as 2>&1 can solve the issue. But I would like to know why ssh command error message would suppress the echo command.
Note that this only happens with ssh command, the following script, which just removes some local files, would outputs both "Removing file" and the "No such file or directory".
#!/bin/bash
rm_file() {
echo "Removing file..."
rm ./$1
}
for file in test1 test2 test3 test4 test5; do
rm_file $file &
done
Add 2>&1 to ssh command, to that message and error stay in order:
ssh $1 'rm ~/test_file' 2>&1
Solution after discussion:
rm_remote_file() {
echo "Removing file on node $1: $( ssh $1 'rm ~/test_file' 2>&1 )"
}
This question already has answers here:
How to get error output and store it in a variable or file
(3 answers)
Closed 8 years ago.
I am trying to store error meesage of a copy command in to a variable. But its not happening
Unix Command
log=`cp log.txt`
cp: missing destination file operand after `log.txt'
Try `cp --help' for more information.
echo $log
<nothing displayed>
I want to store above error message into a variable so that i can echo it whenever i want
Just redirect the stdout (normal output) to /dev/null and keep the stderror:
a=$(cp log.txt 2>&1 >/dev/null)
See an example:
$ a=$(cp log.txt 2>&1 >/dev/null)
$ echo "$a"
cp: missing destination file operand after ‘log.txt’
Try 'cp --help' for more information.
The importance to >/dev/null to keep away to normal output that in this case we do not want:
$ ls a b
ls: cannot access a: No such file or directory
b
$ a=$(ls a b 2>&1)
$ echo "$a"
ls: cannot access a: No such file or directory
b
$ a=$(ls a b 2>&1 >/dev/null)
$ echo "$a"
ls: cannot access a: No such file or directory
Note the need of quoting $a when calling it, so that the format is kept. Also, it is better to use $() rather than , as it is easier to nest and also is deprecated.
What does 2>&1 mean?
1 is stdout. 2 is stderr.
Here is one way to remember this construct (altough it is not entirely
accurate): at first, 2>1 may look like a good way to redirect stderr
to stdout. However, it will actually be interpreted as "redirect
stderr to a file named 1". & indicates that what follows is a file
descriptor and not a filename. So the construct becomes: 2>&1.
Very simple:
Open a console, and type
mkdir abc > output.txt
it creates a file called output.txt which is empty.
now repeat
mkdir abc > output.txt
This displays on the command window:
A subdirectory or file abc already exists
but does not output this to the txt.
Why?
You're redirecting standard output to your file; errors are typically reported on standard error, which is a different output stream. To redirect standard error, you can do this:
mkdir abc 2> output.txt
Or, if you want to combine both streams and direct them together:
mkdir abc 2>&1 >output.txt
More details here.
This will silently create a folder, if it exists or not.
mkdir abc 2>nul
Because the output of mkdir for the second call goes to stderr instead of stdout.