Parsing a command line parameter in script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a command line parameter in script
# 8  
Old 08-17-2011
Yeah I noticed that and corrected it already. Thanks!
# 9  
Old 08-17-2011
Thanks for the tip, Gary_w. The end result of the date exercise is to concatenate the fields into a date/time stamp of the form "Aug17_1445". Assuming that will work with your code, I'll give it a try.

---------- Post updated at 03:05 PM ---------- Previous update was at 02:47 PM ----------

gary_w, that's pretty slick, but when I concatenate the time fields I get spaces in between the values. Is there a trim function that would clear that up? Also, even though I'm only calling for $hr and $mn, I'm getting seconds as well. The resulting string looks like: Aug17_14 45 30. The old way put it out as Aug17_1445. The only problem I have with it is when the day is less than 2 digits. Then it comes out as Aug 7_1445. It would be nice to clean that up while I'm making adjustments.
# 10  
Old 08-17-2011
Please note this improved example. Using a ksh feature of setting the IFS for a read right before the read command itself, one does not have to save the old IFS and restore it: I just now learned this so thanks!!!

# save date output in a variable.

# Parse the elements into their own variables.
print $todaysdate|read dw mo da time zn yr

print "[$dw][$mo][$da][$time][$zn][$yr]"
# If you really need the time elements split out:
print $time | IFS=":" read hr mn sc

print "[$hr][$mn][$sc]"


print $timestamp

$ timetest

Note: I believe my previous example had spaces in the time field as the entire $time field ended up in $hr. It didn't parse on the IFS of ":". I believe it is because in the pipe the read actually runs in a subshell that doesn't know about the IFS changing. I could be wrong (I was wrong once before). At any rate doing it the improved way gets rid of that problem. :-)

Last edited by gary_w; 08-17-2011 at 05:23 PM..
This User Gave Thanks to gary_w For This Post:
# 11  
Old 08-17-2011
It works like a charm. Thanks a bunch.
# 12  
Old 08-18-2011
Simplify even more by eliminating having to parse the time separately. Format the date command like this:
date '+%a %b %e %H %M %S %Z %Y'

Thu Aug 18 09 20 20 EDT 2011

and change the read command as appropriate.

---------- Post updated at 12:43 PM ---------- Previous update was at 09:25 AM ----------

---------- Post updated at 12:44 PM ---------- Previous update was at 12:43 PM ----------

Doh! The simplest way of all:
timestamp=$(date '+%b%e_%H%M')

$ echo $timestamp

Ok, I'm done. :-)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

2. UNIX and Linux Applications

Passing date parameter on Kshell command line

I need to execute a .ksh from command line. The ksh calls a control file and has 3 parameters. First parameter is a csv file, second the target table in oracle and third parameter is a date parameter. I am attempting the below from the ksh command line {code} => testfile.ksh filname.csv... (1 Reply)
Discussion started by: kobe24
1 Replies

3. Shell Programming and Scripting

perl script - command line parameter

i am a beginner, i want to make a program that takes any command line arguments... and print it out in reverse. ie. if the command line argument is "thanks for helping me" i want it to output "me helping for thanks" :D i have tried using the reverse command, but i cant get it working!! ... (3 Replies)
Discussion started by: bshell_1214
3 Replies

4. Shell Programming and Scripting

How to get the last command line parameter?

"$#" gives the number of command-line arguments. How do you get the last command-line parameter (or any particular one determined by a variable)? I thought it would be "${$#}", but that produces something completely unexpected. (4 Replies)
Discussion started by: dkarr
4 Replies

5. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

6. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies

7. Shell Programming and Scripting

replacing a string in a file with command line parameter

Hello, I am trying to replace a string with a paramter given along with the script. I am replacing application1 to application2 with the script: ./ application2 #!/bin/ksh grep $1 applications.dat 2>&1 >/dev/null echo $1 sed 's/Name:... (5 Replies)
Discussion started by: chiru_h
5 Replies

8. Shell Programming and Scripting

using tab to finish command line parameter

Anyone know how to set it up so that when at command line in unix (specifically solaris 2.5.1), and you hit the tab it will finish the command with the nearest file that matches? AND how to set it up so using up and down arrows access your previous commands? Thanks for all the help here, i've had... (3 Replies)
Discussion started by: kymberm
3 Replies

9. Programming

Command line parameter for C program

I am writing a C program that part of the idea is to using a command line parameter to control not to run certain part of the sub program. I am totally new to C, I do not have any idea how to pass a command line arguments from a C program. Can anyone help ?! Thanks (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies

10. UNIX for Dummies Questions & Answers

Command Line width parameter

can someone please tell me how i can increase the number of characters that can be input on the command line? (2 Replies)
Discussion started by: Scoogie
2 Replies
Login or Register to Ask a Question