Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-20-2012
Registered User
 
Join Date: Jan 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
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  
Old 05-20-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,660
Thanks: 165
Thanked 647 Times in 624 Posts
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  
Old 05-21-2012
Registered User
 
Join Date: Jan 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
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  
Old 05-21-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,574
Thanks: 15
Thanked 441 Times in 426 Posts
Quote:
Originally Posted by jim mcnamara View Post

Code:
FILE_ARG='FILE_NM_?(20120515|20120516)??????_sas.sig'

Quote:
Originally Posted by sethmj View Post
> FILE_ARG=FILE_NM_?(20120515|20120516)??????_sas.sig'
Do you see where you went wrong? (Hint: Highlighted in red above) Or is this a typo??
Sponsored Links
    #5  
Old 05-21-2012
Registered User
 
Join Date: Jan 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
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  
Old 05-21-2012
neutronscott's Avatar
script kiddie
 
Join Date: Jun 2011
Location: Charleston, SC
Posts: 649
Thanks: 18
Thanked 189 Times in 179 Posts
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.sig

edit: 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  
Old 05-21-2012
Registered User
 
Join Date: Jan 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
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!

Moderator's Comments:
Please use code tags

Last edited by Scott; 05-21-2012 at 04:14 PM.. Reason: Code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 08:18 AM.