query about shell

General support questions
Post Reply
knzzz
Posts: 157
Joined: 2017/02/25 12:41:42

query about shell

Post by knzzz » 2018/02/21 09:11:54

Hi Team,

I would like to take backup of a folder in case if the folder already exist it should display as folder exists
-----------------------------------------------------------------------------------------------
dt=`date +%F`
present='/tmp/home_$dt'

if [ ! -d "$present" ] ; then
`cp -pr /home/dtail/testing/global/script/home /tmp/home_$dt`
sleep 8
else

echo "Already /home folder present "

fi
-------------------------------------------------------------------------------------------------
even though the backup folder created its not displaying the message Already folder present

stevemowbray
Posts: 519
Joined: 2012/06/26 14:20:47

Re: query about shell

Post by stevemowbray » 2018/02/21 09:39:58

Replace the single quotes around /tmp/home_$dt with double quotes (") so that $dt gets expanded.

Remove the backticks around the cp.

dcrdev
Posts: 70
Joined: 2015/10/25 23:42:17

Re: query about shell

Post by dcrdev » 2018/02/21 09:41:00

Because single quotes are interpreted literally i.e. variables aren't expanded....

Code: Select all

dt="$(date +%F)"
present="/tmp/home_$dt"

if [ ! -d "$present" ]; then
    cp -pr "/home/dtail/testing/global/script/home" "/tmp/home_$dt"
    sleep 8
else
    echo 'The /home folder is already present!'
fi

Post Reply