1835272 Members
2333 Online
110078 Solutions
New Discussion

Urgent Help in Scripting

 
network_4
Advisor

Urgent Help in Scripting

Hi, Below is one bash script in which i want to copy one file from dir A to Dir B and if its get successsful then it will remove that file from source directory


#!/usr/bin/bash
cp /home/admin/atul/OBC/nit1 /home/admin/atul/KBC/nit1
$var = `echo "$?"` ####### Checking the status of previous command
if [$val = 0 ]; then
rm /home/admin/atul/OBC/nit1
else
echo "Copy Failed";
fi
+++++++++++++++++++++++++++++++++++++++++

but while executing script i am getting this error:

ssmove.sh: line 3: =: command not found
ssmove.sh: line 4: [: -eq: unary operator expected
Copy Failed

5 REPLIES 5
SANTOSH S. MHASKAR
Trusted Contributor

Re: Urgent Help in Scripting

Hi,

If u want to copy and delete after success why
don't u use mv command.

$ mv /home/admin/atul/OBC/nit1 /home/admin/atul/KBC/nit1


Also while setting variables don't use $ sign.
and don't use spaces on either side of =.
--$var = `echo "$?"` ####### Checking the status of previous command

Use

var=`echo "$?"` ####### Checking the status of previous command.
And no need to store $? in var, u can directly compare $? like following

if [ $? -eq 0 ]; then
Also

--if [$val = 0 ]; then

since $? returns a number, to compare a number u have to use -eq instead of =,
because = compares strings.

-Santosh
Hemmetter
Esteemed Contributor

Re: Urgent Help in Scripting

Hi

I think you need just a between
"[" and "$val".

another way:

# cp A B && rm A || echo ERROR.



rgds
HGH
Peter Godron
Honored Contributor

Re: Urgent Help in Scripting

Hi,
not a fan of bash, so this is my guess:
1. line 3
val = `echo "$?"`
2. line 4
if [ $val -eq 0 ]

Although I think line 3 should be val = $?


Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

So far you have only awarded points to 1 of 33 answers !
network_4
Advisor

Re: Urgent Help in Scripting

Thanks.. Issue closed
Matti_Kurkela
Honored Contributor

Re: Urgent Help in Scripting

Line 3: when storing a value to a variable, you must not put "$" in front of the variable name. As is, when the variable expansion is done, this command line expands to:

= `echo "0"`

which looks like you're trying to run a command named "=". That's why the error message is "command not found".

Line 4: you're using an uninitialized variable, for two reasons.
1) the previous command did not succeed
2) you have a typo: in the line 3, you store to "var", here you request the value of "val".
If a variable might have an empty value, you should always put double quotes around it.
After variable expansion, this command line expands to:

if [ = 0 ]; then

The error message comes from the "[" command (also known as "test", usually implemented as shell internal command but available as a separate binary too). For it "-eq" and "=" are synonymous, so it displays "=" as "-eq".

If the expression between the brackets begins with an operator, it must be the kind of operator that takes only one argument, i.e. an unary argument. As the unquoted variable expanded to nothing at all, it broke your expression.

Your script also seems over-complex.
You could do it more simply like this:

#!/bin/sh
if cp /home/admin/atul/OBC/nit1 /home/admin/atul/KBC/nit1
then
rm /home/admin/atul/OBC/nit1
else
echo "Copy Failed"
fi

The "if" command can be used to test whether the return code of any command is 0 or not-0.

The "[" command evaluates the expression given to it and sets its return code according to the truth value of the expression.

Combining these two creates the familiar "if [ ... ]" structure.
MK