boundless08
boundless08

Reputation: 303

Bash script returning "Not found" error

I'm writing a bash script to get the latest file from a directory for backup purposes. Here is the script:

#!/bin/sh

set -u
set -e

backup_dir=/media/backup

cd $backup_dir

tar_file= $(ls -Art | tail -n 1)

#ls -Art | tail -n 1

echo $tar_file

When I run the script it gets the right file but also returns a not found error and I don't know why:

./backup: 10: 20130403-120001.tar.gz: not found

I tested it with the line that's commented out, not putting it as a variable and that works without throwing an error so it should work.

Upvotes: 0

Views: 872

Answers (2)

Davide
Davide

Reputation: 508

Is it correct to have

tar_file= $(ls -Art | tail -n 1)

and not

tar_file=$(ls -Art | tail -n 1)

?

(mind the space)

Upvotes: 2

choroba
choroba

Reputation: 241858

Remove the space after the =:

tar_file=$(ls -Art | tail -n 1)

With the space, the line is interpreted as assigning nothing to $tar_file and running the command that is obtained as the output of the $(...). That command is not found.

Upvotes: 3

Related Questions