Changes to write output to logfile and console


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Changes to write output to logfile and console
# 1  
Old 07-28-2014
Changes to write output to logfile and console

Friends,

Below is the script which writes output to LOGFILE, however I want the entire log written to LOGFILE and also console.

Please suggest me the changes I need to do here.

Code:
#!/bin/ksh
x=${0##*/}
LOGFILE="x.log"
echo "CAUTION : Files once deleted cannot be restored"
printf 'Would you like to continue? ("yes" or "no"): '
read INPUT
case "$INPUT" in
(yes)   while read -r i
        do      date -u "+ %Y%m%d %H:%M  GMT -[*] - Deleting files $i" >> \
                        $LOGFILE
                rm -r "$i" 2>> $LOGFILE && echo "File Deleted" >> $LOGFILE
        done < deletefiles.txt;;
(no)    date -u '+ %Y%m%d %H:%M GMT - [!] - Deleting files cancelled' >> \
                $LOGFILE;;
(*)     date -u '+ %Y%m%d %H:%M GMT - [!] - Invalid choice, choose again' >> \
                $LOGFILE;;
esac

# 2  
Old 07-28-2014
Try removing all append redirections and add: esac | tee $LOGFILE.
# 3  
Old 07-28-2014
If you have the tee command, replace >> $LOGFILE with tee -a $LOGFILE and it should direct the output both to the console and the log file.
# 4  
Old 07-28-2014
I have tried first option and throw a syntax error:
./testnew.sh
CAUTION : Files once deleted cannot be restored
Would you like to continue? ("yes" or "no"): yes
./testnew.sh[7]: syntax error at line 13 : `date' unexpected

Here is the edited script:
Code:
#!/bin/ksh
x=${0##*/}
LOGFILE="x.log"
echo "CAUTION : Files once deleted cannot be restored"
printf 'Would you like to continue? ("yes" or "no"): '
read INPUT
case "$INPUT" in
(yes)   while read -r i
        do      date -u "+ %Y%m%d %H:%M  GMT -[*] - Deleting files $i"
                rm -r "$i" 2
        done < deletefiles.txt;;
(no)    date -u '+ %Y%m%d %H:%M GMT - [!] - Deleting files cancelled'
(*)     date -u '+ %Y%m%d %H:%M GMT - [!] - Invalid choice, choose again'
esac | tee $LOGFILE

# 5  
Old 07-28-2014
The ;; at the end of each case statement is not optional, put back the two missing ones Smilie
# 6  
Old 07-28-2014
And you need to redirect rm's error output to stdout for it to be printed to the log file: rm -r "$i" 2>&1
# 7  
Old 07-28-2014
Friends,

I was able to get ouput on console and log file, however the output in log file is not being updated but re-written. Guess we need to have >> at step to append output to be written in log file every time the script runs. Below is the updated script now.

Code:
#!/bin/ksh
x=${0##*/}
LOGFILE="x.log"
echo "CAUTION : Files once deleted cannot be restored"
printf 'Would you like to continue? ("yes" or "no"): '
read INPUT
case "$INPUT" in
(yes)   while read -r i
        do      date -u "+ %Y%m%d %H:%M  GMT -[*] - Deleting files $i"
                rm -r "$i" 2>&1
        done < deletefiles.txt;;
(no)    date -u '+ %Y%m%d %H:%M GMT - [!] - Deleting files cancelled';;
(*)     date -u '+ %Y%m%d %H:%M GMT - [!] - Invalid choice, choose again';;
esac | tee $LOGFILE

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh and Oracle stored procedure output in logfile

Friends, I pass some runtime arguments (date, number) through ksh script to Oracle procedure, use input value and pass it on to procedure. Oracle procedure gets input value, run query and logs everything in the logfile. I'm facing with couple of challenges 1. Even though I pass all... (5 Replies)
Discussion started by: homer4all
5 Replies

2. Shell Programming and Scripting

Logfile monitoring with logfile replacement

Bonjour, I've wrote a script to monitor a logfile in realtime. It is working almost perfeclty except for two things. The script use the following technique : tail -fn0 $logfile | \ while read line ; do ... some stuff done First one, I'd like a way to end the monitoring script if a... (3 Replies)
Discussion started by: Warluck
3 Replies

3. Shell Programming and Scripting

Have each subshell write stderr and stdout to its own logfile

Hello, As stated in the title, I do some hacked parallel processing by running multiple instances of bash scripts, each in their own subshell. The code looks like this, # launch one batch-train script in background for each value in fold group list for FOLD_GROUP in "${FOLD_GROUP_LIST}" do ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

4. Shell Programming and Scripting

Echo cannot redirect first or second output to logfile

I created a script to do some work. I want to use "echo" to redirect "date" to log file. echo works to screen. But cannot redirect first or second "echo" output to logfile. Please help. My code looks like: STARTTIME=`date +%m-%d-%Y` LOGFILE=/directory/logfile.log echo "Start time:" $STARTTIME... (8 Replies)
Discussion started by: duke0001
8 Replies

5. Solaris

No Console Output

Hey everyone I have a sparc enterprise T2000 I'm trying to install solaris 10 on. The only way I can connect to it is the SER MGT console, but I'm not getting anything to display (in Hyper terminal, or PUTTY) when I boot it. Upon googleing, all I get back is "No output may have been generated.... (4 Replies)
Discussion started by: goodvikings
4 Replies

6. Shell Programming and Scripting

how to create a logfile to track the below script output

Hi Dudes, Can you please suggest me how to create a logfile to track the below script output ? Thanks #!/bin/ksh # backup the "std" I/P file descriptor exec 5<&0 #echo "Proceed ?" while read config_line; do # backup the I/P file descriptor of "while" block exec 6<&0 # restore the... (2 Replies)
Discussion started by: shirdi
2 Replies

7. Shell Programming and Scripting

Parse Logfile output variable

<SUMMARY filecount_excluded="0" dirbytes_sent="3367893" dirbytes_hashcache="13275664" ..and so on..> <session numthreads="1" type="avtarbackup" ndispatchers="1" ..and so on..><host numprocs="4" speed="900" osuser="root" name="ashsux01" memory="24545" /><build time="11:04:53" msgversion="13-10" ... (11 Replies)
Discussion started by: Ikon
11 Replies

8. Shell Programming and Scripting

WRITE a message in console

Hi all, I have a requirement, where I need to do some calculations and based on the results I need to write a message to the user from a shell script. Is it possible to `write' inside the script? Something like the below one.. #! /bin/ksh write user12 << EOF Hello World <<EOF For... (2 Replies)
Discussion started by: guruparan18
2 Replies

9. Shell Programming and Scripting

To write to console and file

Hi, How can i redirect the traces to both the console and to a file.. The way i was doing was: /home/harika/harika/samp.exe if ; then echo "executed the first exe.." >> /dev/console;/home/harika/harika/trace.txt /home/harika/harika/memset.exe echo "executed the... (1 Reply)
Discussion started by: harikamamidala
1 Replies

10. UNIX for Dummies Questions & Answers

How do i access (mount, read & write) a floppy disk from the console, not being root?

welll, the title quite explains what i want to do thanks for your time! (4 Replies)
Discussion started by: kfaday
4 Replies
Login or Register to Ask a Question