Append bash timer to file - bash

I am struggling to both provide a decent timer to a user and append the value in the log. I'm not sure why the following loop isn't logging using exec.
My ideal situation is to add >> log.txt to the echo line, but that breaks the visual timer to the user.
One workaround is to write a redundant echo line after the first:
Some progress was made
exec > >(tee log.txt) 2>&1
SECONDS=0
date1=`date +%s`
while ! [ $((`date +%s` - $date1)) -gt 10 ]; do
echo -ne "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)\r"
#Redundant echo
echo "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)" >> log.txt
done

You could pipe the output to tee -a which will send it to standard output and append to log.txt:
echo "$(date -u --date #$((`date +%s` - $date1)) +%H:%M:%S)" | tee -a log.txt

Related

i can't run bash script in other script

This is my script in bash which I added to user crontab, and the command bash /home/lobby/start.sh doesn't work. Can someone help me with this?
#!/bin/bash
if ps aux | grep lobby | grep "Ss" | grep "SCREEN"; then
pkill -f lobby
screen -wipe
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer dzialal Uruchomiono pomyslnie" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer dzialal blad podczas uruchamiania" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
else
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer nie dzialal uruchomilem" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer nie dzialal blad podczas uruchomienia" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
fi

BASH - Correctly quoting a date command

In the process of writing a bash script to parse a tab-separated file and unfortunately I need the ask the user for a date outside of the contents/creation of the file. I've gotten everything working, except looping through badly entered dates from the user until they enter one matching the desired format.
My debug code-block is as follows...
Code:
#!/bin/bash
USER_INPUT="01-01-2011" # ARBITRARILY ASSIGNING A BAD DATE BECAUSE I'M TOO LAZY TO TYPE ONE IN EACH TIME
EXPERIMENTDATE="$USER_INPUT"
if [[ $OSTYPE == *"linux"* ]]
then
date -d \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1
else
date -j -f \"%Y-%m-%d\" \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1
fi
is_valid="$?"
echo -e "$is_valid"
# FYI - $? RETURNS A BINARY FLAG ON THE LAST COMMAND'S EXECUTION. 1 IF ERROR, 0 IF NORMAL EXIT
while [ $is_valid -ne 0 ]; do
echo -e "Invalid date entered. Please enter the day the experiement was conducted on, in exactly the following format. YYYY-MM-DD (e.g. 2011-04-22)"
read USER_INPUT
EXPERIMENTDATE=$USER_INPUT
echo -e "You entered $EXPERIMENTDATE"
if [[ $OSTYPE == *"linux"* ]]
then
echo -e "DEBUG: date -d \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1"
date -d \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1
else
echo -e "DEBUG: date -j -f \"%Y-%m-%d\" \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1"
date -j -f \"%Y-%m-%d\" \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1
fi
is_valid="$?"
echo -e "DEBUG: $is_valid"
done
echo -e "You entered $EXPERIMENTDATE"
From the above, none of the date commands seem to evaluate correctly within the if's, but executing the debug commands directly on the CLI work.
I'm sure this is going to be a quoting/back-tic deal, but I can't seem to figure it out.
You don't need to escape the quotes that aren't inside similar quotes. For example, not
date -j -f \"%Y-%m-%d\" \"$EXPERIMENTDATE\" +%Y-%m-%d > /dev/null 2>&1
but
date -j -f "%Y-%m-%d" "$EXPERIMENTDATE" +%Y-%m-%d > /dev/null 2>&1

Redirection based on variables assigned within while loop setup

I want to access while loop variable out side the loop
while read line
do
...
...
...
done < $file > /home/Logs/Sample_$line_$(date +"%Y%m%d_%H%M%S").log
In the above example whatever the log file is getting generated that doesn't have the value for the line variable. i.e. $line is not working here.
Please let me know how this can be written to make it 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
do
{
START_TIME=`date +%s`
printf "\n SQL File Executed Is : $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 ]; then
echo "Time taken to execute sqlfile $SQL_FILE=$TT_HRS:$TT_MINS:$TT_REM_SECS HH:MM:SS" > $TEMPFILE
printf "\n Success: Sql completed successfully at `date` \n"
cat $TEMPFILE|mailx -s "Time taken to 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 at `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 to execute the 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 actually need redirection inside the while loop:
while read -r line; do
{ cmd1; cmd2; cmd3; } > "/home/Logs/Sample_${line}_$(date +"%Y%m%d_%H%M%S").log"
done < "$file"
When you have > outfile after done then output is redirected to one file only.

Implementing a datalogger in bash

Hi I'm a newby in Bash scripting.
I need to log a data stream from a specific IP address and generate a logfile for each day as "file-$date.log" (i.e at 00:00:00 UT close the previous day file and create the correspondig to the new one)
I need to show data stream on screen while it is logged in a file
I try this solution but not works well because never closesthe initial file
apparently the condition check never executes while the first command of the pipe it is something different to an constant string like echo "something".
#!/bin/bash
log_data(){
while IFS= read -r line ; do printf '%s %s\n' "$(date -u '+%j %Y-%m-%d %H:%M:%S')" "$line"; done
}
register_data() {
while : ;
do
> stream.txt
DATE=$(date -u "+%j %Y-%m-%d %H:%M")
HOUR=$(date -u "+%H:%M:%S")
file="file-$DATE.log"
while [[ "${HOUR}" != 00:00:00 ]];
do
tail -f stream.txt | tee "${file}"
sleep 1
HOUR=$(date -u "+%H:%M:%S")
done
> stream.txt
done
}
nc -vn $IP $IP_port | log_data >> stream.txt &
register_data
I'll will be glad if someone can give me some clues to solve this problem.

How to read a file content using shell script

cat file.txt
(
while read line
do
initial_time=`head -n 1 $line| sed -e 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/'`
initial_time_sec=`date -d "$initial_time" +%s`
done
)
I want to take the date from every line , if it is less than 60 days I have to store it in variable
while read line ; do
initial_time=`echo $line | sed -e 's/.*\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/'`
initial_time_sec=`date -d "$initial_time" +%s`
now=`date +%s`
pass_time=`expr $now - $initial_time_sec`
limit_time=$((60 * 24 * 60))
if [[ $pass_time -lt $limit_time ]]; then
echo $line
fi
done <data

Resources