|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Variable with Special characters
Hello all, I am facing with a problem of invoking an environment variable. If I use this command : Code:
/bin/ls -lt FILE_NM_?(20120515|20120516)??????_sas.sig | head -n1 | awk '{print $9}'It perfectly outputs the desired result. Code:
FILE_NM_20120516000000_sas.sig But if I do this: Code:
FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig and then do this: Code:
/bin/ls -lt $FILE_ARG | head -n1 | awk '{print $9}'The last code doesnt work. Any help is appreciated! Last edited by Franklin52; 05-21-2012 at 04:42 AM.. Reason: Please use code tags |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
This is file matched by the shell at the time you create it. Code:
FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig This is not Code:
FILE_ARG='FILE_NM_?(20120515|20120516)??????_sas.sig' This last example is then used this way: Code:
ls -l $FILE_ARG which expands the variable into a valid filename. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Hi Jim, Thank-you for the response! Here's how I tried: Code:
> FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig' and then Code:
> /bin/ls -lt $FILE_ARG | head -n1 | awk '{print $9}'The message I am getting is: FILE_NM_?(20120515|20120516)??????_sas.sig not found Even though there are 2 files available in the same directory. Am I doing something wrong here! Last edited by Franklin52; 05-21-2012 at 04:43 AM.. Reason: Please use code tags |
|
#4
|
||||
|
||||
|
Quote:
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
BALAJESURI, Sorry about this, but it was a typo in the posting and not the actual code. The acual code did had: Code:
FILE_ARG='FILE_NM_?(20120515|20120516)??????_sas.sig' I am still facing the same error: Code:
FILE_ARG_?(20120515|20120516)??????_sas.sig not found Thank-you for the response! Last edited by Scott; 05-21-2012 at 04:13 PM.. Reason: Code tags |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
What shell are you using? I'm only famaliar with bash, but I think you're using ksh. Seems the expansions are done in different order. bash: Code:
[mute@geek ~/temp/sethmj]$ shopt -s extglob [mute@geek ~/temp/sethmj]$ FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig [mute@geek ~/temp/sethmj]$ echo "$FILE_ARG" FILE_NM_?(20120515|20120516)??????_sas.sig [mute@geek ~/temp/sethmj]$ ls -lt $FILE_ARG -rw-r--r-- 1 mute mute 0 May 21 15:46 FILE_NM_20120515000000_sas.sig -rw-r--r-- 1 mute mute 0 May 21 15:44 FILE_NM_20120516000000_sas.sig ksh: Code:
$ FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig $ echo "$FILE_ARG" FILE_NM_?(20120515|20120516)??????_sas.sig $ ls -lt $FILE_ARG ls: cannot access FILE_NM_?(20120515|20120516)??????_sas.sig: No such file or directory $ ls -lt FILE_NM_?(20120515|20120516)??????_sas.sig -rw-r--r-- 1 mute mute 0 May 21 15:46 FILE_NM_20120515000000_sas.sig -rw-r--r-- 1 mute mute 0 May 21 15:44 FILE_NM_20120516000000_sas.sig Neither does filename expansion on variable assignment. Except with arrays: Code:
$ files=( FILE_NM_?(20120515|20120516)??????_sas.sig ); echo "${files[@]}"
FILE_NM_20120515000000_sas.sig FILE_NM_20120516000000_sas.sigedit: If you're looking for the latest file matching the glob, try: Code:
unset latest for file in FILE_NM_?(20120515|20120516)??????_sas.sig do [[ $file -nt $latest ]] && latest=$file done echo "$latest" Last edited by neutronscott; 05-21-2012 at 12:07 PM.. |
| The Following User Says Thank You to neutronscott For This Useful Post: | ||
sethmj (05-21-2012) | ||
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
hello Neurtonscott, It works fine... but can you explain what this code is doing? unset latest Code:
for file in FILE_NM_?(20120515|20120516)??????_sas.sig do [[ $file -nt $latest ]] && latest=$file done Especially the switches and the content withing the do loop!
Last edited by Scott; 05-21-2012 at 04:14 PM.. Reason: Code tags |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Replace special characters with Escape characters? | laknar | Shell Programming and Scripting | 8 | 01-05-2012 11:40 PM |
| Sed failing for variable with special characters | sasiharitha | Shell Programming and Scripting | 7 | 11-21-2011 05:41 PM |
| Special characters in a bash variable in sed | linuxnewbeee | Shell Programming and Scripting | 3 | 04-13-2009 08:59 AM |
| Variable Manimpulation - special characters | Nomaad | Shell Programming and Scripting | 1 | 04-04-2008 05:17 PM |
| Special characters getting replaced by &Pound in Unix Environment | kaushik05 | UNIX for Advanced & Expert Users | 0 | 09-23-2005 01:10 AM |
|
|