Need help on 2>&1

I need help. I'm trying to have the out of en error not only show up on the screen , but also be copied into a file.

This is part of my script

file="`date +%b_%d_%y:%H:%M:%S`"
touch $file | mv $file ~/errors/idtoolerrorlogs | cp ~/errors/idtoolerrorlog ~/errors/idtoolerrorlogs/$file
 
 
~/errors/idtoolerrorlog > ~/errors/idtoolerrorlogs/$file 2>&1
 

All this does is print the result on the screen. But it does not print in the file. Any thoughts

You can pipe your command to tee so you get output on screen and also on a file.

<some commands> 2>&1 | tee log.out

With 2>&1 you are redirecting STDERR to STDOUT. From the first look of the script, it seems to be broken. Can you please post the whole script with CODE tag around it? I do not see anything where you are feeding errors to STDERR.

~/errors/idtoolerrorlog > ~/errors/idtoolerrorlogs/$file 2>&1

The above line should not work as idtoolerrorlog is a text file and you need to use cat command to print its content and then redirect.

Please post the full script to help you better.

#!/bin/bash
 
#Linux Script
 
echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID
 
echo "`date +%D:%H:%M:%S`" ; read -p "Enter Server(s)"= servertext && echo $servertext > serverlist
 
cat serverlist
for i in `cat serverlist`
do
echo "$i"
 
echo "`date +%D:%H:%M:%S`" ; ssh -t -t $i id $UserID
 
cat /dev/null > serverlist
 
file="`date +%b_%d_%y:%H:%M:%S`"
 
touch $file | mv $file ~/errors/idtoolerrorlogs | cp ~/errors/idtoolerrorlog ~/errors/idtoolerrorlogs/$file
 
~/errors/idtoolerrorlog ~/errors/idtoolerrorlogs/test 
 
 done
echo "`date +%D:%H:%M:%S`" ; ssh -t -t $i id $UserID

You are forcing to allocate tty on remote server for ssh session; have you configured ssh key sharing?

cat /dev/null > serverlist

What is the point of including this in the loop? I don't see any point of zeroing out a file again and again in a loop.

touch $file | mv $file ~/errors/idtoolerrorlogs | cp ~/errors/idtoolerrorlog ~/errors/idtoolerrorlogs/$file

Combining commands using pipe!! I have not seen anyone using pipe for this sort of thing. Plus, with this, commands are executed right-to-left. Have you ever considered using "&&" instead?

~/errors/idtoolerrorlog ~/errors/idtoolerrorlogs/test 

I do not see idtoolerrorlog a script file or a binary executable. How do you even expect this to be working? Are you trying to copy the idtoolerrorlog file to test? If yes, where is the cp command in front?

Ohh yes, by some magical way, the code that you posted earlier (pasted below) vanished!! I am wondering why!!

~/errors/idtoolerrorlog > ~/errors/idtoolerrorlogs/$file 2>&1

No offense buddy, but, the script seems to be pointless and error prone from top to bottom.

Can you tell me what exactly you are trying to do? Maybe I can write the script for you.

I took a look at my script yesterday and I revised it.

!/bin/bash

Linux Script

 echo "`date +%D:%H:%M:%S`" ; read  -p  " User ID"= UserID

  echo "`date +%D:%H:%M:%S`" ; read -p "Enter Server(s)"= servertext && echo $servertext > serverlist

  cat serverlist
  for i in `cat serverlist`
                        do
         echo "$i"
echo "`date +%D:%H:%M:%S`" ;  ssh -t -t $i id $UserID

 cat /dev/null > serverlist
 
 file="`date +%b_%d_%y:%H:%M:%S`"

 touch $file 

 mv $file ~/errors/idtoolerrorlogs  

~/idtool  2>  ~/errors/idtoolerrorlogs/$file 
 
 
 

                      done
 
 

To answer to some of your questions:

echo "`date +%D:%H:%M:%S`" ; ssh -t -t $i id $UserID

You are forcing to allocate tty on remote server for ssh session; have you configured ssh key sharing?

I don't have rights on the to configure ssh key sharing

cat /dev/null > serverlist

What is the point of including this in the loop? I don't see any point of zeroing out a file again and again in a loop.

I'm try to delete the contents of serverlist.

What I posted before is what I need help. But if you'd like to build me a new script, please feel free.

---------- Post updated at 01:20 PM ---------- Previous update was at 01:11 PM ----------

Sorry. The script is :

took a look at my script yesterday and I revised it.

#!/bin/bash

#Linux Script

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

echo "`date +%D:%H:%M:%S`" ; read -p "Enter Server(s)"= servertext && echo $servertext > serverlist

cat serverlist
for i in `cat serverlist`
do
echo "$i"
echo "`date +%D:%H:%M:%S`" ; ssh -t -t $i id $UserID

cat /dev/null > serverlist

file="`date +%b_%d_%y:%H:%M:%S`"

touch $file 

mv $file ~/errors/idtoolerrorlogs 

~/idtool 2> ~/errors/idtoolerrorlogs/$file

There is lots of redundancy and pointless things in here.

#!/bin/bash

#Linux Script

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

# You don't need a file.
# echo "`date +%D:%H:%M:%S`" ; read -p "Enter Server(s)"= servertext && echo $servertext > serverlist

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext

# Just use the variable itself.  That's what it's there for.
for i in $servertext
do
        echo "$i"
        echo "`date +%D:%H:%M:%S`"
        ssh -t -t $i id $UserID
        file="`date +%b_%d_%y:%H:%M:%S`"

        # You don't need to touch the file.
        # touch $file 
        # You don't need to move the file.
        # mv $file ~/errors/idtoolerrorlogs

        # This is all that's needed.
        ~/idtool 2> ~/errors/idtoolerrorlogs/$file

        # If you want it to append, not overwrite, do this:
        # ~/idtool 2>> ~/errors/idtoolerrorlogs/$file
done

I have another question. Lets say the scripts name is "idtoolflinux" and I want to copy any errors form the output of this script (when I say output I mean as I press the command ./idtoolflinux and see the errors on the screen) to file mention below ; how would I go about doing it?

Could I use the script :

~/idtoolflinux 2> ~/errors/idtoolerrorlogs/$file

assuming $file is anything, then yes. It will overwrite the file every time you call it though.

Another thing you can do is capture them all at once.

exec 2> logfile
# cause an error message
touch /
$ cat logfile
touch: setting times of `/': Permission denied

I do like the "exec 2> ~/errors/idtoolerrorlogs/$file" command (without the quotation marks). The problem is the error messege shows up in the file and not on the screen. Is there anyway have the error error message show up on the screen and in the file?

You can't redirect one message into two file descriptors, no. The closest you can get is weird convolutions with tee and a fifo.

You could do this at the top of the script, though. tail -f watches your logfile and prints as it sees changes. Then once you're done, it kills it.

trap "kill $PID" EXIT
exec 2> logfile
tail -f logfile &
PID=$!

It's getting this error:

 
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Show your code.

#!/bin/bash
#Linux Script
trap "kill $PID" EXIT
exec 2> ~/errors/idtoolerrorlogs/$file
tail -f ~/errors/idtoolerrorlogs/$file &
PID=$

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext
for i in $servertext
do
echo "$i"
echo "`date +%D:%H:%M:%S`"
ssh -t -t $i id $UserID
file="`date +%b_%d_%y:%H:%M:%S`"

Thank you.

PID=$

should be

PID=$!

I couldn't have seen that error without you posting your code.

Ok, here is the whole code with the change

#!/bin/bash
#Linux Script
file="`date +%b_%d_%y:%H:%M:%S`"

         trap "kill $PID" EXIT 
         exec 2&gt; ~/errors/idtoolerrorlogs/$file
         tail -f ~/errors/idtoolerrorlogs/$file &
         PID=$!

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext
for i in $servertext
do
echo "$i"
echo "`date +%D:%H:%M:%S`"
ssh -t -t $i id $UserID

done

The results:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

So I use single quotes in the "kill" command.

#!/bin/bash
#Linux Script
file="`date +%b_%d_%y:%H:%M:%S`"

         trap 'kill $PID' EXIT 
         exec 2&gt; ~/errors/idtoolerrorlogs/$file
         tail -f ~/errors/idtoolerrorlogs/$file &
         PID=$!

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext
for i in $servertext
do
echo "$i"
echo "`date +%D:%H:%M:%S`"
ssh -t -t $i id $UserID

tixuw #Also added this just to recreate an error

done

Well , the error was trapped in the "$file" but it not show up on the screen and I was still able to continue with the script. So, if this is as far as we can go then thank you very much for your help. This is my first time created a series of major scripts and this really showed me how much more I have to learn. But if there is another way to pull of looking at the error on the screen and putting in a file a the same time, please show me how. Thanks again.