1

I'm writing a script that needs to find a file in a directory based on the user input. That file contains a filepath, and I need to use that filepath as a variable so I can use it later in a mv command. So far :-

read x
path = `cat ~/filepaths/$x`

Later it needs to move a file from trash using the filepath read from this file

mv ~/trash/$x $path

Currently, it doesn't appear to work, and hangs when it runs. Is there something stupid I've missed here?

EDIT: Solved, was a stupid syntax mistake. Thanks for your help!

Jordan Moffat
  • 337
  • 6
  • 17
  • 1
    http://stackoverflow.com/questions/2268104/basic-bash-script-variable-declaration-command-not-found – William Pursell Nov 27 '12 at 01:10
  • It's "hanging" because `read` is waiting for input. You'll get a syntax error on line 2 as soon as read gets data. – William Pursell Nov 27 '12 at 01:11
  • Also, you should consider using the `$(subshell command)` syntax instead of the `\`subshell command\`` syntax. It's typically less error-prone. – jedwards Nov 27 '12 at 01:16
  • 2
    @WilliamPursell: line 2 is a syntactically correct invocation of a program called `path` with argument 1 set to `=` and arguments 2 onwards set to whatever is in the file `~/filepaths/$x`. – Jonathan Leffler Nov 27 '12 at 01:35

1 Answers1

7

Remove the spaces around the assignment:

path=`cat ~/filepaths/$x`

or:

path=$(< ~/filepaths/$x)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
perreal
  • 94,503
  • 21
  • 155
  • 181