Environment Variable with Special characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Environment Variable with Special characters
# 1  
Old 05-20-2012
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 05:42 AM.. Reason: Please use code tags
# 2  
Old 05-20-2012
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.
# 3  
Old 05-21-2012
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 05:43 AM.. Reason: Please use code tags
# 4  
Old 05-21-2012
Quote:
Originally Posted by jim mcnamara
Code:
FILE_ARG='FILE_NM_?(20120515|20120516)??????_sas.sig'

Quote:
Originally Posted by sethmj
> 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??
# 5  
Old 05-21-2012
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 05:13 PM.. Reason: Code tags
# 6  
Old 05-21-2012
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 01:07 PM..
This User Gave Thanks to neutronscott For This Post:
# 7  
Old 05-21-2012
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:
Mod Comment Please use code tags

Last edited by Scott; 05-21-2012 at 05:14 PM.. Reason: Code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

2. Shell Programming and Scripting

Parsing special characters from variable commands

Hi, I am fairly new to unix scripting and recently tasked with some reporting scripts. The reporting checks several batch jobs and this is quite iterative. Now I am trying to minimize script effort and maximize reusability as there are only slight nuances in the repetitive tasks. For... (3 Replies)
Discussion started by: joeniks
3 Replies

3. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

6. Shell Programming and Scripting

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

7. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

8. Shell Programming and Scripting

Special characters in a bash variable in sed

Hello, I am trying the following: echo __CHANGEME__ >> testfile VAR1="&&&" sed -i "s|__CHANGEME__|${VAR1}|" testfile cat testfile This results in testfile containing __CHANGEME____CHANGEME____CHANGEME__ Whereas I want it to result in &&& I understand that if VAR1="\&\&\&" then... (3 Replies)
Discussion started by: linuxnewbeee
3 Replies

9. Shell Programming and Scripting

Variable Manimpulation - special characters

Hello- I have a variables that contains a string like this usr/pass@SCHEMA I want to extract the usr/pass part and ignore the SCHEMA part, I tried to use this ${dbconn%%@} and apparently it will not work because @ is a special character. I tried \@ and still no go. Any idea how to solve... (1 Reply)
Discussion started by: Nomaad
1 Replies

10. UNIX for Advanced & Expert Users

Special characters getting replaced by &Pound in Unix Environment

Hi, Please find the Question Summary below- In our email template document(.txt) bullets and Apostrophe are getting replaced by the string "&pound" in our Live environment.We are using sun solaris 8 in live. Can anybody let me know why this happens and how to prevent this . Thanks... (0 Replies)
Discussion started by: kaushik05
0 Replies
Login or Register to Ask a Question