Puzzle: file name grepped from text file yields "no such file"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Puzzle: file name grepped from text file yields "no such file"
# 1  
Old 01-27-2011
Question Puzzle: file name grepped from text file yields "no such file"

Dear all,
In a bash script, I grep a filename from an UTF8 encoded file:
Code:
LIST=`grep ^source $FILE | tr "\t" " " | cut -d " " -f 2 | sed -e 's,~,\$HOME,g'`

The result is
Code:
# echo $LIST
$HOME/.mail_aliases_seminaire_MMMG

Then I try to access it:
Code:
#ls $LIST
ls: cannot access $HOME/.mail_aliases_seminaire_MMMG: No such file or directory

while in the same script I can access the file hard coding its name
Code:
ls $HOME/.mail_aliases_seminaire_MMMG
/home/me/.mail_aliases_seminaire_MMMG

I wondered whether it's an encoding effect, however:
Code:
#echo $LIST | od -t oC -An > /tmp/CS
#echo '$HOME'/.mail_aliases_seminaire_MMMG | od -t oC -An | sdiff  - /tmp/CS
 044 110 117 115 105 057 056 155 141 151 154 137 141 154 151      044 110 117 115 105 057 056 155 141 151 154 137 141 154 151 
 163 145 163 137 163 145 155 151 156 141 151 162 145 137 115      163 145 163 137 163 145 155 151 156 141 151 162 145 137 115 
 115 107 012                             115 107 012

Any idea?
# 2  
Old 01-27-2011
May be the shell did not expand the variable. Can you try as
Code:
ls "$LIST"
or 
eval ls "$LIST"

This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 01-27-2011
Ah...when you have a variable which contains the name of another variable, that wont automatically be evaluated. So LIST contains $HOME, and you cant use that directly.
I think this article should help.
This User Gave Thanks to citaylor For This Post:
# 4  
Old 01-27-2011
Cheers ! Adding 'eval' did the trick.


And thanks Citaylor for your explanation and refs.

Btw, ~ counts as a variable, I hadn't originally the sed bit to replace it with $HOME and the same issue was showing already.

Last edited by josce; 01-27-2011 at 05:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

replace the contents of first column of file "X" with second Column of file "X" in file "Y"

Hi! I am having 02 files. In first file" X" I am having 02 Columns TCP-5100 Sybase_5100 TCP-5600 Sybase_5600 Second file "Y" for example-- :services ( :AdminInfo ( :chkpf_uid ("{A2F79713-B67D-4409-83A4-A90804E983E9}") :ClassName (rule_services) ) :compound ()... (12 Replies)
Discussion started by: shahid1632
12 Replies

3. UNIX for Dummies Questions & Answers

Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All It's me again with another huge txt files. :confused: What I have: - I have 33 huge txt files in a folder. - I have thousands of line in this txt file which contain many the letter "x" in them. - Some of them have more than one "x" character in the line. What I want to achieve:... (8 Replies)
Discussion started by: Nexeu
8 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

"Content-type: text/html No input file specified." Only in CRON but not when executed directly

Hi gang, I have the following code inside a the file script.sh #!/bin/bash todaysdate=$(date --date='7 day' +'%d') todaysmonth=$(date +'%m') todaysyear=$(date +'%Y') yahoodatestring=$todaysyear$todaysmonth$todaysdate nicedate=$(date --date='5 day' +'%A') nice="$nicedate,... (2 Replies)
Discussion started by: phpchick
2 Replies

6. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Identify high values "˙" in a text file using Unix command

I have high values (such as ˙˙˙˙) in a text file contained in an Unix AIX server. I need to identify all the records which are having these high values and also get the position/column number in the record structure if possible. Is there any Unix command by which this can be done to : 1.... (5 Replies)
Discussion started by: devina
5 Replies

9. Fedora

Piping output of "top" to a text file

I would like to pipe "top -n" to a text file, but I get an error: top: cannot open /dev/kmem kvm_open: Permission denied I am a non-root user. If I could find a way to get this type of output: "Memory: 2048M real, 1516M free, 4099M swap free" into a text file, I could further automate... (13 Replies)
Discussion started by: safraser
13 Replies

10. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies
Login or Register to Ask a Question