I have a simple script that builds a complex program call which passes a number of parameters to the program. I'm trying to enhance the script to include the value of the command line parameter in the name of a file being created. The problem I'm having is that the parameter may include a forward slash (/) which is not valid in filenames. Ideally I would like to replace any slashes with underscores. My script is called "sasi" and it will require a parameter which may contain 0 to n slashes. The first part of the parameter should be an 8-character string. If changing the slashes is too complicated, I'd be happy with capturing just the first 8 characters. I've been trying to use the 'cut' function to strip off the first 8, but it's not working. 'cut' may not be the best approach, but I think it will do the job if I get the syntax correct. However, I'm open to suggestions for better methods. The script begins like this:
#!/usr/bin/ksh
#
# UNIX script to initiate interactive SAS session,
# log and lst files are saved as "session_mmddhhmn"
# in the lstsave and logsave directories
#
log_mo=`date | cut -c5-7`
log_da=`date | cut -c9,10`
log_dw=`date | cut -c1-3`
log_hr=`date | cut -c12,13`
log_mn=`date | cut -c15,16`
log_sc=`date | cut -c18,19`
log_zn=`date | cut -c21-23`
log_yr=`date | cut -c25-28`
eval 'parm=\${1} | cut -c1-8'
echo &parm
When I try to run it, I get the message "parm: not found". Any assistance would be most appreciated.
could this be caused by the &parm in the last line? It should be $parm.
At the first glance, I don't believe what you are trying is going to work. But let's take 1 step at a time. Let me know once you have corrected the above issue.
Save yourself a bunch of overhead by calling date once:
#!/bin/ksh
# save date output in a variable.
todaysdate=$(date)
# 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:
OLDIFS=$IFS
IFS=:
print $time|read hr mn sc
IFS=$OLDIFS
print $hr $mn $sc
The read command will parse on the Input Field Seperator.
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.
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!!!
#!/bin/ksh
# save date output in a variable.
todaysdate=$(date)
# 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]"
timestamp="${mo}${da}_${hr}${mn}"
print $timestamp
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.