Command line output not redirecting to file. - windows

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.

Related

bash : cat commands output only if it success

I'm trying to redirect a command's output in a file only if the command has been successful because I don't want it to erase its content when it fails.
(command is reading the file as input)
I'm currently using
cat <<< $( <command> ) > file;
Which erases the file if it fails.
It's possible to do what I want by storing the output in a temp file like that:
<command> > temp_file && cat temp_file > file
But it looks kinda messy to me, I want avoid manually creating temp files (I know <<< redirection is creating a temp file)
I finally came up with this trick
cat <<< $( <command> || cat file) > file;
Which will not change the contents of the file... but which is even more messy I guess.
Perhaps capture the output into a variable, and echo the variable into the file if the exit status is zero:
output=$(command) && echo "$output" > file
Testing
$ out=$(bash -c 'echo good output') && echo "$out" > file
$ cat file
good output
$ out=$(bash -c 'echo bad output; exit 1') && echo "$out" > file
$ cat file
good output
Remember, the > operator replaces the existing contents of the file with the output of the command. If you want to save the output of multiple commands to a single file, you’d use the >> operator instead. This will append the output to the end of the file.
For example, the following command will append output information to the file you specify:
ls -l >> /path/to/file
So, for log the command output only if it success, you can try something like this:
until command
do
command >> /path/to/file
done

How to create a file if it does not exist otherwise clear an existing file?

I have a project that opens a file and writes some information to it. The file needs to exist, and needs to be empty. The file needs to be created the first time the program is run, and the file needs to be cleared for each successive run.
Is there an easy way in bash to create a file if it does not exist, else clear the contents of the existing file?
You don't need to use echo or any external utility, but just use the shell built-ins. Just use the shell re-direct operator (> file).
$ mkdir testDir
$ cd testDir/
$ ls -l
total 0
$ > fileThatDoesNotExist
$ ls -1
fileThatDoesNotExist
It will work even if there are some contents in the file, it will truncate its content
$ echo 'whatever' > fileThatDoesNotExist
$ cat fileThatDoesNotExist
whatever
$ > fileThatDoesNotExist
$ cat fileThatDoesNotExist

Bash echo output redirected to log file in specified directory

I'm getting the both right results but I'm also getting a additional weird result. A file is being created for each file that I'm downloading in the directory of script being executed and not in to log directory. When I commented the echo out, it goes away and the files are not created. Is there another way or what is the correct way for me to log the address that I'm downloading into CURL?
echo $DLADDR$'\r' >> Downloads/LOGS/$LOGFILE 2>$1
curl -o Downloads/$FILECATNAME $DLADDR >> Downloads/LOGS/$LOGFILE 2>&1
You should change that 2>$1 into 2>&1. Otherwise stderr will be redirected into a file of the name "$1" (first argument to the script).

storing error message of command output into a shell variable [duplicate]

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.

how to print proper error message

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.

Resources