0

I want to write a script for both interactive and batch use. If arguments are not provided the script will ask for the users input.

Unlike here the user shouldn't be bothered if the variable is already defined by arguments.

Using parameter expansion I tried this:

${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"}

... but I always get a command not found error.

Any suggestions?

Community
  • 1
  • 1
Horst
  • 334
  • 1
  • 12
  • Try the following script. echo "Enter your name" ; read name; if [ ${#name} -eq 0 ] then echo -e "\nName: Horst" else echo -e "\nName: $name" fi – mohangraj Jul 30 '15 at 11:48
  • Just answered it on my own: `FILE=${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"}` It's some sort of double assingment, but it works! – Horst Jul 30 '15 at 11:48
  • Try This: echo ${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"} – mohangraj Jul 30 '15 at 11:58
  • Stop answering questions in the comments. – chepner Jul 30 '15 at 12:46

2 Answers2

1

I would write

# earlier in program, $FILE may or may not be defined

if [[ -z $FILE ]]; then
    read -p "Please provide the file: " FILE
fi

If you really want to cram it into one line, use the : command and the assignment expansion syntax

: ${FILE:=$(read -p "file?"; echo "$REPLY")}

Note, don't use ALL_CAPS_VARS: someday you'll use PATH and wonder why your script is broken.

Community
  • 1
  • 1
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

It should be

FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"

You are trying to execute the command instead of initialization

${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"}
Please provide the file:
Mahesh
-bash: Mahesh: command not found
$ FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"
Please provide the file:
mahesh
$ echo $FILE
mahesh
Mahesh Kharvi
  • 389
  • 2
  • 6