shell - Redirection based on variables assigned within while loop setup -


i want access while loop variable out side loop

while read line ... ... ... done < $file > /home/logs/sample_$line_$(date +"%y%m%d_%h%m%s").log 

in above example whatever log file getting generated doesn't have value line variable. i.e. $line not working here.

please let me know how can written make work.

#!/bin/sh exec 1> /home/logs/`basename $0 | cut -d"." -f1 | sed 's/\.sh//g'`_$(date +"%y%m%d_%h%m%s").log 2>&1 echo "execution started : `date` \n" sql_dir=/home/sql infile=in_file tempfile=temp_file return_code=0 ls -ltr $sql_dir|grep ".sql"|awk -f" " '{print $9}'|awk -f"." '{print $1}' > $infile sed '/^$/d' $infile > $tempfile; mv $tempfile $infile  while read line       {         start_time=`date +%s`          printf "\n sql file executed : $line.sql"         sql_file_nm=$line.sql         sql_file=$sql_dir/$sql_file_nm         nzsql -db netezza_db -atqf $sql_file > /dev/null          return_code=$?         if [ $return_code -eq 0 ];                 echo "time taken execute sqlfile $sql_file=$tt_hrs:$tt_mins:$tt_rem_secs hh:mm:ss" > $tempfile                 printf "\n success: sql completed @ `date` \n"                 cat $tempfile|mailx -s "time taken execute sqlfile $sql_file=$tt_hrs:$tt_mins:$tt_rem_secs hh:mm:ss" 'koushik.chandra@a.com'         else                 printf "\n error: failed in sql execution @ `date`"                 exit $return_code         fi          end_time=`date +%s`         tt_secs=$(( end_time - start_time))         tt_hrs=$(( tt_secs / 3600 ))         tt_rem_ms=$(( tt_secs % 3600 ))         tt_mins=$(( tt_rem_ms / 60 ))         tt_rem_secs=$(( tt_rem_ms % 60 ))         printf "\n"         printf "total time taken execute sql $line="$tt_hrs:$tt_mins:$tt_rem_secs hh:mm:ss         printf "\n"       } > /home/logs/sql_query_time_$line_$(date +"%y%m%d_%h%m%s").log  done < $infile  rm -f $infile $tempfile exit $return_code 

you need redirection inside while loop:

while read -r line;    { cmd1; cmd2; cmd3; } > "/home/logs/sample_${line}_$(date +"%y%m%d_%h%m%s").log" done < "$file" 

when have > outfile after done output redirected 1 file only.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -