Dhargombeths
Dhargombeths

Reputation: 31

Make a program USB Copyer with bash shell linux

This is my code that I made to copy all fd data to the home/user directory with bash shell

#!/bin/sh
user=$(whoami)
now=$(date +"%d.%m.%Y_%H.%M.%S")
dir=$(mkdir $now) --> and here I want to create a folder as a place to copy the results according to datetime

if [ -d /media/$user/foo ]
then
    echo "any media"
    cp -r /media/$user /home/$user/$dir
else
    echo "none of media attached"
fi

but i run thats code, cp -r /media/$user /home/$user/$dir not running as well, which is not copied to the newly created directory based on datetime , any advice ? or something's wrong in my code ?

Upvotes: 0

Views: 46

Answers (1)

Cyrus
Cyrus

Reputation: 88654

I suggest to replace

dir=$(mkdir $now)

with

mkdir "$now" || exit 1
dir="$now"

This will terminate your script if there is a problem with the directory creation.

Upvotes: 1

Related Questions