Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing directory as a variable for later use in linux script

Tags:

linux

bash

In my script, I am holding the location (path) of a file as a variable.

For example, fileA

An example of its contents are

fileA=/usr/anotherfolder/somefold/"filenamehere"

However, when i call a command on the file in the script such as:

cat $fileA

or

cat "$fileA"

I get an error saying the file or directory doesn't exist. If I echo $fileA to see what the output is, and then run a cat manually from the terminal, it works fine, don't know what is going wrong. Any help?

Some debug info:

  • fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
  • echo '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' /home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"
  • '[' '!' -r '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' ']'

For these particular lines

Check for readable file

echo $fileA
if [ ! -r "$fileA" ]
then
    o=`expr $o + 1`
    echo "$fileA not readable."
    continue    
fi
like image 525
jsjwooowooo Avatar asked Apr 13 '12 05:04

jsjwooowooo


1 Answers

If file name is new(not "new"), then change

fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'

to

fileA=/home/jacob/Desktop/CS35L/WORK/2/hw/test3/new
like image 62
kev Avatar answered Nov 02 '22 19:11

kev