Passing Arguments to shell script from file is not working as expected.

Hi All,

I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination.

# file_copy.sh
source_dir =  ${source_dir}
target = ${target_dir}
cp source_dir target

and my parameter file is like below

#parameter_file.txt
source_dir = "/home/cloudera/source/source_files.txt"
target_dir = "/home/cloudera/target/test.txt"

I am using below command to execute this script

sh file_copy.sh $(< parameter_file.txt)

after executing I am getting below error

ftp.sh: line 3: source_dir: command not found
ftp.sh: line 4: target: command not found
cp: cannot stat `source_dir': No such file or directory

Can anyone suggest where I am doing mistake?

Thanks,
LakshmiNarasimha

Shell script syntax does not allow space in assignments, so:

source_dir =  ${source_dir}

should be

source_dir=${source_dir}

Also, reading the parameter file does not work that way..
You can use the source or . command to do this, but also here the should be no spaces in assignments in that source file.

source ./parameter_file

Instead of sourcing the parameter file, you can also get the information with the read command.

Lastly, if you want to reference a shell variable, you need to prepend a $-sign.

1 Like

On top of what Scrutinizer already said,

source_dir=${source_dir}
target=${target_dir}

are two expensive No-Ops, as the value of source_dir is replaced by the value of source_dir etc. So, the entire file_copy.sh script would reduce to

cp $source_dir $target_dir

, or, with a sanitized parameter file

. parameter_file.txt
cp $source_dir $target_dir
1 Like

Hi RudiC/Scrutinizer,

Thank you for the inputs.
As per your replay I changed my code like below and getting expected results.

# file_copy.sh
cp  ${source_dir} ${target_dir}
#parameter_file.txt
source_dir = "/home/cloudera/source/source_files.txt"
target_dir = "/home/cloudera/target/test.txt"
source file_copy.sh parameter_file.txt

Thanks,
LakshmiNarasimha

As we don't know what ftp.sh nor ftp_parameters.txt are, we can't comment on their functionality. Certainly they don't have much in common with file_copy.sh nor parameter_file.txt , as the syntax errors Scrutinized commented on are still around in the latter, and we don't see the former used anywhere. Maybe, if ftp.sh contains sourcing its first positional parameter (which needs to be sanitized, as repeated before), and then calls / sources file_copy.sh , we get to something working.

1 Like