0

I'm a beginner. :) I'm trying to ask the name of file from prompt in a shell and edit that file in another shell like this:

test.sh

echo "enter file name"
read word
sh test2.sh

test2.sh

read number
echo "$number" >> $word

I get an error

Test2.sh: line 1: $mAmbiguous redirect 

Any suggestion?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Leili
  • 1
  • 2
  • possible duplicate of [Getting an "ambiguous redirect" error](http://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error) – tripleee Apr 10 '15 at 05:28

1 Answers1

0

If you want a variable from test.sh to be visible to its child processes, you need to export it. In your case, you would seem to want to export word. Perhaps a better approach would be for test2.sh to accept the destination file as a parameter instead, though.

test.sh

echo "enter file name"
read word
test2.sh "$word"

test2.sh

#!/bin/sh
: ${1?must have destination file name}  # fail if missing
read number
echo "$number" >> "$1"
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You need to have it in your `PATH`, or use `./test2.sh`. Obviously, the file needs to have execute permission (`chmod +x ./test2.sh`). – tripleee Apr 10 '15 at 05:59