2014-07-27 2 views
0

나는 다음과 같은 bash는 스크립트가 있습니다 bash 스크립트의 다음 매개 변수가 제대로 작동하지 않는 이유는 무엇입니까?

#!/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 

는 기본적으로 myPlotter.R는 주어진 파일의 데이터에서 하나의 플롯을 만드는

(자세한 내용은이 질문에 대한 중요하지 않습니다)를. 다음과 같은 방법으로 명령 줄에서 호출 할 때 : 스크립트는 (,, myFile에 (34) (30)주의를 지불하지 않습니다 잘 작동
./myPlotter.R myFile "My Title" 30 34 

, 그들은 단지 다른 매개 변수하지만,이 질문에 대한 중요하지 않습니다 내가 그들을 떠났다. 이 경우에 대비해). 같은 bash는 스크립트에서 호출 할 때, :

./bashPlot.sh -1 myFile -2 "My Title" -3 30 -4 34 

나는 오류 메시지 수 (인수에서 오류 [3] : 인수 [4], 도움이 경우 "강제 도입의 NA"). 실행하는 경우 :

echo ./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP 

나는이 제목은 두 개의 인수로 ('내'와 '제목') 말아야 때 계산되는 것을 의미하는 다음

./myPlotter.R myFile My Title 30 34 

처럼 보이는 것으로 나타났습니다. 그래서

./myPlotter.R $fileWithSpeeds \"$titleOfGraph\" $lowestHP $highestHP 

에 줄을 수정하기로 결정 라인의 echo는 준 :

./myPlotter.R myFile "My Title" 30 34 

을하지만 난 여전히 같은 오류로 끝납니다. 제 생각에이 제목은 여전히 ​​두 개의 인수 ('My'및 'Title')입니다. 이 문제를 해결할 수있는 방법이 있습니까?

#!/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") 

답변

2

당신은 할 일이 있는지 감지하는 것입니다. 인용

fileWithSpeeds="$OPTARG" #... and such 

./myPlotter.R "$fileWithSpeeds" "$titleOfGraph" "$lowestHP" "$highestHP" 
+0

어쩌면 "는"문제가되지 않지만 여기에 결정적 "일"문제 :

사용하므로, 모든 곳에서 인용. –

0

./myPlotter.R $fileWithSpeeds "$titleOfGraph" $lowestHP $highestHP 

이 문제를 해결

./myPlotter.R $fileWithSpeeds \"$titleOfGraph\" $lowestHP $highestHP 

에 변경 : 다음의 경우 도움이 R 스크립트입니다. 그것은 단지 내 실수였습니다. 당신이

bash -x bashplot.sh .... arguments.... 

당신에게 스크립트를 디버깅하는 경우

관련 문제