argument not passed correctly to the remote bash script - bash

So I've got this script that runs on rhel 5.8 and what it does is that it reads two arguments, should be dates in yyyy-mm-dd format, and fires a script on a remote server, sunOS 5.10. In both sides the shell I'm using is /bin/bash
The part of the code that's not working as expected is the below:
...
CC="IE"
read -p "Please provide start date (format yyyy-mm-dd): " STARTDATE
read -p "Please provide end date (format yyyy-mm-dd): " ENDDATE
echo ""
echo "Start Date provided :" $STARTDATE
echo "End date provided :" $ENDDATE
echo ""
echo "Executing query on the remote side, please wait..."
ssh -t user#rem_host '/tmp/rem_script.sh '${CC}' '${STARTDATE}' '${ENDDATE}' ' >/dev/null 2>&1
What I get when set -x on the local host is the following
+ read -p 'Please provide start date (format yyyy-mm-dd): ' STARTDATE
Please provide start date (format yyyy-mm-dd): 2014-05-08
+ read -p 'Please provide end date (format yyyy-mm-dd): ' ENDDATE
Please provide end date (format yyyy-mm-dd): 2014-05-09
+ echo ''
+ echo 'Start Date provided :' 2014-05-08
Start Date provided : 2014-05-08
+ echo 'End date provided :' 2014-05-09
End date provided : 2014-05-09
+ echo ''
+ echo 'Executing query on the remote side, please wait...'
Executing query on the remote side, please wait...
+ ssh -t user#rem_host '/tmp/rem_script.sh IE 2014-05-08 2014-05-09 '
But what I see on the remote side being executed is the following:
user 730 688 0 10:30:31 pts/2 0:00 bash -c /tmp/rem_script.sh IE 2014-05-08 2014-05
Can anyone please tell me why the last date is being chopped off? How can I correct this?
Thanks in advance...

Related

Calculate number of days between 2 dates Unix Shell

I want to calculate number of days between two dates in the unix shell .
I tried to do a minus calculation but it dosen’t work .
This is my script
VAR1=$1
VAR2=$2
v_date_deb=`echo ${VAR1#*=}`
v_date_fin=`echo ${VAR2#*=}`
dif = ($v_date_deb - $v_date_fin)
echo dif
if [ "$v_date_deb" = "" ]
then
echo "Il faut saisir la date debut.."
exit
fi
if [ "$v_date_fin" = "" ]
then
echo "Il faut saisir la date fin.."
exit
fi
One attempt (but shot in the dark, since we don't know what is in your VAR1 variables)
ts1=$(date -d "${VAR1#*=}" +"%s")
ts2=$(date -d "${VAR2#*=}" +"%s")
dt=$(( (ts2 - ts1) / 86400 ))
Note the remark from William Pursell above: this solution is dependent on your "date" version. Date is not a built-in command from bash. And, particularly, the -d option (that allows to use the date specified instead of the current date that date is supposed to use otherwise, when used in "print the date" mode) is not common to all "date".

Shell Script piping

#!/bin/sh
output=ANIL;
#
# Ask user for database inputs
#
echo -n "Enter Database Server Hostname: "
read dba_host
echo -n "Enter Database SID: "
read dba_sid
echo -n "Enter DBA User: "
read dba_usr
echo -n "Enter DBA password: "
read dba_pwd
echo -n "What daemon are we using: "
read daemon_str
#
# Loop to connect to database and exit if something is found
#
while :
do
output=`sqlplus $dba_usr/$dba_pwd#$dba_sid <<+ | grep '^-' | sed 's/-//'
set serveroutput on
DECLARE
command VARCHAR2(50);
return_name VARCHAR2(30);
value VARCHAR2(10000);
status INT;
system_time TIMESTAMP := SYSTIMESTAMP;
WHILE TRUE
BEGIN
status := DBMS_PIPE.RECEIVE_MESSAGE($daemon_str);
IF status = 0 THEN
DMS_PIPE.UNPACK_MESSAGE(command);
END IF;
IF command = "STOP" THEN
DBMS_OUTPUT.PUT_LINE('STOP was encountered') >> file.log;
DBMS_OUTPUT.PUT_LINE(system_time) >> file.log;
BREAK
ELSIF command = "SYSTEM" THEN
DBMS_PIPE.UNPACK_MESSAGE(return_name);
DBMS_PIPE.UNPACK_MESSAGE(value);
EXIT
$value
ELSIF command = "SQL"
DBMS_PIPE.UNPACK_MESSAGE(return_name);
DBMS_PIPE.UNPACK_MESSAGE(value);
EXECUTE IMMEDIATE value;
ELSE
nap(10)
EXCEPTION
WHEN OTHERS THEN
dbms_ouput.put_line('Unknown Input Error') >> file.log;
DBMS_OUTPUT.PUT_LINE(system_time) >> file.log;
DBMS_PIPE.PACK_MESSAGE('done');
DBMS_PIPE.PACK_MESSAGE(status);
status := DBMS_PIPE.SEND_MESSAGE(return_name,10);
END;
dbms_output.put_line(chr(10) || '-' || command);
END;
/
exit
+`
echo $output
done
I am trying to convert a c code block to what you see now a shell script block. I am just a beginner in this coding language and was wanting to know if anyone is seeing something I am not. To sum up what I am trying to accomplish is ask user for the oracle database they want to connect to then keep connection and receive things through pipe. Then the usual of unpacking, outputting errors and such. Then sending it back through same pipe. Any input on possible syntax or anything at all that could be causing this to constantly echo out nothing but blank lines from while loop.

script weird behaviour with ampersand at end

I am writing a script where I face a very strange problem.
My script is :
#!/bin/bash
set -x
cd /scripts/my-scripts
echo "Getting data from database."
mysql -N -umyuser -pmypass -hdb.test.example.com COLLEGE -e"select distinct id as myid from candidate_info where name = 'john' and id is not null " > test_skus &
echo "DONE"
echo `wc -l test_skus`
It gives 0 as word count. But if I remove & at end of this :
mysql -N -umyuser -pmypass -hdb.test.example.com COLLEGE -e"select distinct id as myid from candidate_info where name = 'john' and id is not null " > test_skus &
It give results. What can be the reason ? Please help

BASH script checking log files for current and previous month

I have been working on this on and off for the last two months, and despite how many times I look at it, I can't make it work.
This script checks daily log files for a user defined variable, so they don't have to look through every one manually. It worked great checking the current month, but if the user wants to check back 20 days, and today is the 12th of this month, I wanted to be able to then go back to the previous month (not look for the log file with a date of 20150399 and so on). I have checked the logic for my date/day computations, and they seem okay (if there is a better way to do that in BASH, I am open to suggestions). What happens when I try to debug is unexpected end of file. I am somewhat new to writing scripts that contain more than 20 or so lines, but I just can't come up with what I am missing.
I have tried various fixes, to no avail, but I think this is the last iteration.
Ideas?
#!/bin/bash
########################################################
# multi_log_chk.sh
# This script will take input from the user and report which
# CyberFusion MFT logs contain what the user is looking for.
# Hopefully this will save the user having to search through every
# stinking log file to find what they are looking for.
# 20150406 pxg007 started typing
# 20150413 pxg007 added && comparison for back out (line 28)
# added message for no entries found (line 32, 38, 48-52)
# Added some further description (line 16)
# 20150424 pxg007 Added logic to calculate previous month and if necessary, year. (Lines 16-24, 60-78 )
#
########################################################
currDate=`date +%d%B%C%y`
currDay=`date +%d`
currMnth=`date +%m`
currYear=`date +%C%y`
case $currMnth in #Let's establish number of days for previous month
05 | 07 | 10 | 12 ) lastMnthD=30;;
01 |02 | 04 | 06 | 09 | 08 | 11 ) lastMnthD=31;;
03 ) lastMnthD=28;; ##and screw leap year
esac
if [ $currMnth -eq 01 ]; then ##accounting for January
lastMnth=12
else
lastMnth=$((currMnth-1))
fi
if [ $lastMnth -eq 12 ]; then ## accounting for Dec of previous year
lastMnthYr=$((currYear-1))
else
lastMnthYr=$currYear
fi
echo "This script will find entries for your query in whatever available MFT logs you request."
echo " "
echo "For instance - how many log files have transfer entries with \"DOG\" in them?"
echo " "
echo "I also will also give an estimate of how many transfers per log file contain your query, give or take a couple."
echo " "
echo "This search is case sensitive, so \"DOG\" is *** NOT *** the same as \"dog\""
echo " "
read -p "What text you are looking for? Punctuation is okay, but no spaces please. " looking ### what we want to find
echo " "
echo "Today's date is: $currDate."
echo " "
read -p "How many days back do you want to search(up to 25)? " daysBack ### How far back we are going to look
if [ "$daysBack" == 0 ] && [ "$daysBack" >> 25 ]; then
echo "I said up to 25 days. We ain't got more than that!"
exit 1
fi
echo " "
echo "I am going to search through the last $daysBack days of log files for:\"$looking\" "
echo " "
read -p "Does this look right? Press N to quit, or any other key to continue: " affirm
if [ "$affirm" = N ] && [ "$affirm" = n ]; then ###Yes, anything other than "N" or "n" is a go
echo "Quitter!"
exit 1
else
nada=0 ### Used to test for finding anything
backDate=$((currDay-daysBack)) ### current month iterator (assuming query covers only current month)
if (("$daysBack" => "$currDay")); then ## If there are more logs requested than days in the month...
lastMnthCnt=$((daysBack-currDay)) ### how many days to check last month
lastMnthStrt=$((lastMnthD-lastMnthCnt)) ## last month start and iterator
backDate=$(currDay-(daysBack-lastMnthCnt)) # Setting the iterator if we have to go back a month
while (("$lastMnthStrt" <= "$lastMnthD" )); do
foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$lastMnthYr$lastMnth$lastMnthStrt" | parsecflog | wc -l )
howMany=$((foundIt/40+1)) ### Add one in case there are less than 40 lines in the record.
if (("$foundIt" > 0))
then
nada=$((nada+1))
echo "Log.txt.$lastMnthYr$lastMnth$lastMnthStrt contains $looking in approximately $howMany transfer records."
lastMnthStrt=$((lastMnthStrt+1))
echo " "
else
lastMnthStrt=$((lastMnthStrt+1))
fi
fi
backDate=$((currDay-daysBack)) ### current month iterator (assuming query covers only current month)
while (("$backDate" <= "$currDay")); do
foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$backDate" | parsecflog | wc -l )
howMany=$((foundIt/40+1)) ### Add one in case there are less than 40 lines in the record.
if (("$foundIt" > 0))
then
nada=$((nada+1))
echo "Log.txt.$backDate contains $looking in approximately $howMany transfer records."
backDate=$((backDate+1))
echo " "
else
backDate=$((backDate+1))
fi
if [ "$nada" \< 1 ]
then
echo " "
echo "I found no entries for $looking in any log file."
fi
You are missing the keyword 'done' on lines 81 and 96 and also a final 'fi' keyword on the last line.
Also as others suggested you can do
date -d "20 days ago" +"%d%B%C%y"
to easily get dates in the past

Updating File Created Date by x number of days Mac OSX

I have a folder of videos (Mac OSX Yosemite) for which I need to change the Created Date by adding 2180 days to the existing Created Date.
Using SetFile from Terminal I am able to manipulate the Created Date, for example I can set it as equivalent to the Modified Date of the same file:
SetFile -d "$(GetFileInfo -m /Users/myfilename.mov)" /Users/myfilename.mov
However, if I try to add the ‘Add 2180 days’ part it stops working:
SetFile -d "$(GetFileInfo -d /Users/myfilename.mov) +2180 days" /Users/myfilename.mov
I suspect it is an issue with bracket and speech marks but the following did not work either:
SetFile -d "$(GetFileInfo -d /Users/myfilename.mov +2180 days)" /Users/myfilename.mov
How exactly should I be incorporating the '+2180 days' into it?
Edi: Mark Setchell has a solution which works but I am keen to know if there is in fact a way to incorporate '+2180 days' into the GetFileInfo-based -d date variable.
That's a lot of fun for something apparently so simple!!!! I think this works, but test it out on some sample files. I left my debugging statements in, but you can safely remove all the echo statements.
#!/bin/bash
# Get name of file as supplied as parameter
file=$1
# Get its timestamp in format "02/08/2015 21:14:44"
timestamp=$(GetFileInfo -d "$1")
echo timestamp:$timestamp
# Convert that to seconds since the Unix Epoch, e.g. 1423430084
epoch=$(date -j -f "%m/%d/%Y %H:%M:%S" "$timestamp" +%s)
echo epoch:$epoch
# Calculate seconds in 2180 days
((offset=2180*3600*24))
echo offset:$offset
# Add offset to epoch
((epoch+=offset))
echo new epoch:$epoch
# Get new date in format that SetFile wants
newdate=$(date -r $epoch "+%m/%d/%Y %H:%M:%S")
echo new date:$newdate
# And finally set the date of the input file
SetFile -d "$newdate" "$file"
Save it as ReDate and make it executable (only necessary once) with
chmod +x ReDate
and run it like this:
./ReDate /Users/myfilename.mov
Sample run:
./ReDate "/Users/Mark/tmp/file with sapce in name.mov"
timestamp:02/09/2015 09:54:01
epoch:1423475641
offset:188352000
new epoch:1611827641
new date:01/28/2021 09:54:01
I know this is an older thread, but I wanted to do something like this. The OP #MrDave is asking about adding via normal speech. This can be accomplished with AppleScript.
A bit verbose, but it works:
on run {input}
set filename to input as text
set fileDate to (creation date of (get info for input))
set newDate to fileDate + (1 * days)
set newMonth to month of newDate as number
set newMonth to lessThanTen(newMonth)
set newDay to day of newDate as number
set newDay to lessThanTen(newDay)
set newYear to year of newDate as number
set newHour to hours of newDate as number
set newHour to lessThanTen(newHour)
set newMinute to minutes of newDate as number
set newMinute to lessThanTen(newMinute)
set divider to "/"
set newSetDate to newMonth & divider & newDay & divider & newYear & " " & newHour & ":" & newMinute as text
set printNewDate to "\"" & newSetDate & "\"" as string
log printNewDate
-- date format for SetFile: mm/dd/yyyy hh:mm
do shell script ("SetFile -d " & printNewDate & " -m " & printNewDate & " " & input)
end run
on lessThanTen(num)
set thisNum to num as number
if thisNum is less than 10 then
set newNum to "0" & thisNum
else
set newNum to thisNum
end if
return newNum as string
end lessThanTen
now from terminal run:
osascript /Users/path/to/scriptName.scpt filename
and here you can actually just drag the file from Finder onto the Terminal instead of typing the filename itself, then hit enter

Resources