Reputation: 1278
I have the following bash script:
#!/bin/bash
while getopts "1:2:3:4:" arg; do
case "$arg" in
1)
fileWithSpeeds=$OPTARG
;;
2)
titleOfGraph=$OPTARG
;;
3)
lowestHP=$OPTARG
;;
4)
highestHP=$OPTARG
;;
esac
done
./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP
Basically, myPlotter.R
makes one plot from data in a given file (details are not important for this question). When called in the command line in the following way:
./myPlotter.R myFile "My Title" 30 34
The script works fine (don't pay attention to myFile
, 30, 34; they are just other parameters but not important for this question. I left them there for this question just in case). However, when called from the bash script like:
./bashPlot.sh -1 myFile -2 "My Title" -3 30 -4 34
I get an error message (Error in args[3]:args[4], "NAs introduced by coercion" if helpful). When running:
echo ./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP
I noticed that it looks like the following
./myPlotter.R myFile My Title 30 34
which means that the title is counting as two arguments ('My' and 'Title') when it shouldn't. So I decided to modify the line to
./myPlotter.R $fileWithSpeeds \"$titleOfGraph\" $lowestHP $highestHP
and the echo
of the line gave:
./myPlotter.R myFile "My Title" 30 34
But I still end with the same error. My guess is that the title is still two arguments ('"My' and '"Title'). Is there a way to fix this? Here is the R script if helpful:
#!/usr/bin/env Rscript
# the arguments come in this way:
# args[1] is a file containing the maximum speeds of different cars (one per line)
# args[2] is the title that the plot will have
# args[3] contains the horsepower of the engine of the first car in args[1] (the lowest)
# args[4] contains the horsepower of the engine of the last car in args[1] (the highest)
# NOTE1: the speeds in args[1] must be listed starting from the car
# with the lowest horsepower to the car with the highest horsepower
# NOTE2: in args[1], a car must differ from the next one by 1 horsepower, i.e., if
# there are 5 speeds, and the horsepower of the first car in the file is 30, then the
# the horsepower of the second one must be 31, the third one 32, .... the fifth one must
# be 34.
args<-commandArgs(TRUE)
# creating the vector with the horsepower of each car
horsepowers = numeric()
for (i in args[3]:args[4]) {
horsepowers = c(horsepowers,i)
}
# reading file with speeds and getting vector with speeds
speeds <- read.csv(file=args[1],head=FALSE,sep="\n")$V1
# creating plot with speeds in previous vector
outputTitle = gsub(" ","", args[2] , fixed=TRUE)
pdf(paste(outputTitle, ".pdf", sep = ""))
plot(horsepowers, speeds, type="o", col="red", xlab="horsepowers", ylab="speeds")
# giving a title to the plot
title(main=args[2], col.main="Black")
Upvotes: 0
Views: 57
Reputation: 63972
If you debug you script with
bash -x bashplot.sh .... arguments....
you will able to detect what's happened.
use quotes everywhere, so:
fileWithSpeeds="$OPTARG" #... and such
and
./myPlotter.R "$fileWithSpeeds" "$titleOfGraph" "$lowestHP" "$highestHP"
Upvotes: 2
Reputation: 1278
Changing
./myPlotter.R $fileWithSpeeds \"$titleOfGraph\" $lowestHP $highestHP
to
./myPlotter.R $fileWithSpeeds "$titleOfGraph" $lowestHP $highestHP
fixed the problem. It was just a minor mistake on my part.
Upvotes: 0