I'm trying to write a bash script that will query the current system time (OS X 10.6.6) and then convert the output from HH:MM:SS into time in seconds. The output of the system time command (systemsetup -gettime) is returned as:
Time: HH:MM:SS
so I wanted to use awk -F: to grab HH:MM:SS
From the command line I can:
How do I assign $2, $3, and $4 from awk to the variables hr, min, and sec?
Thank you all in advance!
---------- Post updated at 06:21 PM ---------- Previous update was at 10:26 AM ----------
Sorry, I didn't know how to use the code tags.
I found a different method to capture the hours, minutes, and seconds, so in the above example I now have:
so if ref_time was set to "Time: 22:32:59" then
I would now like to re-assemble hr, min, and sec into a single variable. I tried variations of:
but with everything I've tried echo $set_time still returns 22 32 59 and I need it to return 22:32:59 with the :'s.
Using epoch time is very intriguing because it avoids a lot of the pain of converting back and forth between HH:MM:SS and seconds, but I'm having trouble setting the system time with date and using epoch time. Any reason to use date over systemsetup?
I was able to solve my problem with using a colon as a separator. Earlier in the script I used IFS=":" to parse HH MM SS from systemsetup -gettime. I unset IFS after grabing my init values and everything seems to be working just fine.
Thanks to all for your help and insights!
Last edited by xaiu; 03-13-2011 at 06:28 PM..
Reason: solved problem
Using epoch time is very intriguing because it avoids a lot of the pain of converting back and forth between HH:MM:SS and seconds, but I'm having trouble setting the system time with date and using epoch time. Any reason to use date over systemsetup?
For one thing, you'll find GNU date in Linux as well as OSX. For another thing, I'm guessing that date is a less heavyweight utility than systemsetup.
To get epoch seconds from GNU date, you just do date +%s
To set the date with epoch seconds, I think you do date -s @192342... where 192342... is your epoch seconds of course and @ tells date that it's epoch seconds. Just one of the little things you have to dig real deep into date's info files to find.
---------- Post updated at 01:08 PM ---------- Previous update was at 01:05 PM ----------
Quote:
Originally Posted by xaiu
Earlier in the script I used IFS=":" to parse HH MM SS from systemsetup -gettime. I unset IFS after grabing my init values and everything seems to be working just fine.
You can set IFS for individual lines and nothing else by prefixing the line. Like: IFS=":" read HH MM SS < datafile Though IFS of course can have side-effects on how parameters expand so be careful what you put on the right-hand side.
You can also use IFS to seperate things into arrays for you.
I am going to try incorporating several of the suggestions you made. I have noticed when running the version of the script that I came up with I get an occasional system message that appears to be the result of a pre-dated timestamp somewhere, but otherwise is doing the job for which it was intended. My thinking was that systemsetup would work better with regards to any daemons running int he background, but perhaps date is the right tool for the job? I did have trouble getting the clock to set using date when you originally suggested it, but had not come across the -s @epoch_seconds syntax.
I want to run commands inside a bash script.
An example is
I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable
sed.sh regexp
sed.sh "-i \"s/<p>//g\""
then call
sed "$regexp" $fl (3 Replies)
Hi,
I need to perform the untar and rm operation if the file found is a .tar and does not have test.tar or hello.tar as the file names.
Below is the loop to check the same.
for tf in *.tar
do if ]
then found=1
... (1 Reply)
i have defined a function ln_s() for customizing the ln command in script1.sh.
more script1.sh
echo "Starting Execution"
./script2.sh
echo "End of Execution"
ln_s(){
] && return
ln -s "$1" "$2"
}
My script1.sh executes another script2.sh which has the following entry
more script2.sh... (12 Replies)
i have some script with some paths inside it. The idea is to some files which is on desktop copy and move to another location. Problem is that inside script is similar to this:
cp test1.zip /root/help/
because I allways have another zip files, does it possible to have some input which ask me... (18 Replies)
Hi,
I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh).
I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory
" eve though I merged the whole... (7 Replies)
I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows:
#!/bin/bash
HOST=$1
/usr/bin/expect -c "
spawn ssh -i /root/.ssh/id_rsa root@$HOST
expect -exact "Enter... (3 Replies)
Hi,
I have a bash script to run many system commands on CentOS machine, but I am puzzled by some commands had no effect on parent environment.
For example, I want to refresh the desktop xdg menu when some processes added or deleted items from desktop xdg menu. If I run "killall gnome-panel"... (4 Replies)
I have a file (email) containing email addresses.
I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples:
\.trainee
\.group
\.web
I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)