0

I want to cat or read the contents of file giving absolute path that contains a variable but its not working..

None of the below commands are working.
($line here is the name of file)

content=`(cat /tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/${line})`

cat '/tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/'${line}
cmak.fr
  • 8,696
  • "None of the below commands are working." I recommend you [edit] this question with details about what does happen when you run those commands, including complete exact output. If that doesn't show the actual name of at least one one of the files for which it fails, please include that, too. – Eliah Kagan Jul 21 '19 at 04:53
  • 3
    An example of what $line contains would be helpful. Maybe it already contains the directory name? Maybe it contains spaces? We simply do not know enough to help. – WinEunuuchs2Unix Jul 21 '19 at 18:12
  • Close voters. Crafting answer. – Elder Geek Jul 22 '19 at 13:54
  • You should say what shell/environment you're using. Also, whether you're running this at the command line or via a script? Might be worth giving the whole script, eg on pastebin.com. Also, if you haven't heard of shellcheck I recommend it for debugging shell scripts (eg it will tell you to use $() instead of `` [backticks]). – pbhj Jan 28 '22 at 00:23

3 Answers3

1

If as you say $line contains the name of the file you wish to output , you can simply cat (or more or less) the variable. as in cat $line

A safer method that would work even if the filename includes spaces would be cat "$line"

You should insure that you know the difference between full and relative paths as you aren't likely to get the results you want if you confuse the two.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
0

Variable assignation from cat:

content=$(cat /tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/${line})
# Or
content=`cat /tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/${line}`

cat a file from variable:

cat /tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/${line}
# OR
cat "/tmp/cygwin/cygwin/home/67642360/1.0.94/db/src/sql/oracle/updates/${line}"
cmak.fr
  • 8,696
0

What is ${line}?

Can you try echo ${line}?

What shell are you using? You can set line to be a valid file name. I'm using bash in this example.

foo$ echo ${line}

foo$ echo "Hello World" > /tmp/foo.txt 
foo$ export line=foo.txt
foo$ echo ${line}
foo.txt
foo$ cat /tmp/${line}
Hello World
foo$