using awk inside bash script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using awk inside bash script?
# 1  
Old 03-12-2011
using awk inside bash script?

Hello,

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:

Code:
prompt: systemsetup -gettime ; a=$(systemsetup -gettime) ; echo $a | awk -F: '{print $2":"$3":"$4}'
  Time: 22:32:59 <-- output of systemsetup -gettime
   22:32:59 <-- tells me that I want to capture $2, $3, and $4

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:

Code:
d=$ref_time
IFS=":"
set -- $d
hr=$2
min=$3
sec=$4

# strip leading white space from hr
hr=$(echo $hr | tr -d " ")

so if ref_time was set to "Time: 22:32:59" then
Code:
prompt: echo $ref_time
Time: 22:32:59
prompt: echo $hr $min $sec
22 32 59

I would now like to re-assemble hr, min, and sec into a single variable. I tried variations of:
Code:
set_time=$hr":"$min":"$sec

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.

Any advice?

Last edited by xaiu; 03-12-2011 at 09:37 PM..
# 2  
Old 03-12-2011
this | that | other | awk is overkill for this. And you don't need the systemsetup command either.

Code:
read HH MM SS <<< $(date '+%H %M %S')

and ding, they're in shell variables.
Then you strip off the leading zeroes
Code:
HH=${HH#0}
MM=${MM#0}
SS=${SS#0}

...and add the whole mess together:

Code:
S=$((SS + (MM*60) + (HH*60*60) ))
echo $S


Or, if you want epoch seconds, you can do it all in one with
Code:
date +%s

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-13-2011
Thanks for the tips!

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
# 4  
Old 03-16-2011
Quote:
Originally Posted by xaiu
Thanks for the tips!

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.

Code:
OLDIFS="${IFS}"
IFS=":"
ARRAY=( ${SOMETHING_WITH_COLONS_IN_IT} )
IFS="${OLDIFS}"

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-16-2011
Again, much thanks!

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.

All the best!
# 6  
Old 03-17-2011
The man page for GNU date is almost blank, nearly all the useful information's in the info file. Which is a real pain since info is.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing sed command inside a bash script

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)
Discussion started by: Kangol
3 Replies

2. Shell Programming and Scripting

Need Multiple checks inside if condition in a bash shell script

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)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Aliases NOT working inside bash shell script

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)
Discussion started by: mohtashims
12 Replies

4. Shell Programming and Scripting

Is it possible to change paths inside a bash script?

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)
Discussion started by: tomislav91
18 Replies

5. Shell Programming and Scripting

Run bash command inside zsh script

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)
Discussion started by: kamcamonty
7 Replies

6. Shell Programming and Scripting

Expect not working inside my Bash script

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)
Discussion started by: John Wilson
3 Replies

7. Shell Programming and Scripting

Why commands inside bash script lost effectiveness?

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)
Discussion started by: hce
4 Replies

8. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

running bash command inside awk

Org file 192.168.1.10 d:\adir\xdir 192.168.1.11 d:\bdir\ydir want to covert it into robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\adir robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\bbdir (5 Replies)
Discussion started by: ydk
5 Replies

10. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

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)
Discussion started by: manouche
1 Replies
Login or Register to Ask a Question