Matt
Matt

Reputation: 15

Shell script giving seemingly incorrect exit information

When I run this script with a folder as a parameter I get this error:

lab9.sh: 1: eval: Syntax error: "(" unexpected

But how can this be? My line 1 has no open parenthesis. I thought the problem was my shebang but as you can see it's in perfect condition. Everything else in my script appears to be in order.

#!/bin/bash

file=$1
firstPart='tar -cvf n00866097_Backup('
date='`date +%Y-%m-%d`'
lastPart=').tgz $file'
zippedFile=$firstPart$date$lastPart

eval $zippedFile

pscp -pw password $zippedFile [email protected]:[test]/

Upvotes: 0

Views: 49

Answers (3)

miracle173
miracle173

Reputation: 1973

Of course the error message of the shell is correct. In your script the shell is invoked two times. At first it is invoked when the script is started by running something like ./lab9.sh. During the execution of this script the start of another shell is triggered by eval when exectuting

eval $zippedFile

In this context this newly started shell should execute the statement

tar -cvf n00866097_Backup(`date +%Y-%m-%d`).tgz

For this new shell this statement is the first one, so it's line number is 1. But ( and ) have a special meaning for the shell it is syntactically wrong to place them here and so an error is raised. If you want them as part of the filename you have to escape them. But check the other answers, it is not a good idea to use eval here at all.

Upvotes: 0

agc
agc

Reputation: 8406

There's no need for eval or variables aside from $date:

date=$(date +%Y-%m-%d)
tar -cvf n00866097_Backup\(${date}\).tgz $1

pscp -pw password \
   tar -cvf n00866097_Backup\(${date}\).tgz $1 \
      [email protected]:[test]/

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

You need to quote the filename containing () literals. And you could use $() to execute your date command. And don't mix up the zipFile with the command to create it, or your pscp won't work. Like,

file="$1"
zippedFile="n00866097_Backup($(date +%Y-%m-%d)).tgz"
cmd="tar -cvf \"$zippedFile\" \"$file\""
eval "$cmd"

Upvotes: 1

Related Questions