user3442089
user3442089

Reputation: 11

Confirmation Required regarding script

I am making a small script which zipps a file and then copy it to another dir. But when i execute it; the file gets zipped but on copying it gives following error:

cp: missing destination file operand after /home/centoslive/Desktop/new

following is my script

#!/bin/bash

file=$(gzip jawad.txt)

cp $file /home/centoslive/Desktop/new

Upvotes: 1

Views: 47

Answers (1)

Emmet
Emmet

Reputation: 6411

The problem is that the line:

file=$(gzip jawad.txt)

Captures the output of gzip onto stdout into the variable file. If gzip succeeds, it will write a file jawad.txt.gz, but it generates no output on stdout, so the file variable will be empty.

Then your cp line (after expansion of the empty $file) looks like:

cp /home/centoslive/Desktop/new

Which is missing the destination that cp thinks you want to copy /home/centoslive/Desktop/new to.

Instead, you probably just want to do:

cp jawad.txt.gz /home/centoslive/Desktop/new

You might consider something like:

src=jawad.txt
dst=/home/centoslive/Desktop/new

if gzip $src; then
    cp $src.gz $dst
fi

This way, you only do the copy if the gzip succeeds. You could put an else in there to handle the case when it fails.

Upvotes: 3

Related Questions