3

I am using rsync command to sync two folder and on success of rysnc I want to copy a file success and while copying append source folder name parameter like Success_FolderName.I am using $(basename !:3) to get the third parameter i.e Folder Name.

bash /Sync.sh 10_03_2016

#! /bin/bash

set -o history
set -o histexpand
/usr/bin/rsync -avh -r /Source/$1 /Destination/
rsyncStatus=$?
folderParam=$(basename !:3)
if($rsyncStatus==0)
then
   cp /Status/Sucesss   /Result/Success_$folderParam
else
    cp /Status/Failure   /Result/Failure_$folderParam
if

Output

Error

/Sync.sh: line 7: :3: bad word specifier 

And File gets copied with 'Success_'

3

I don't understand why you're making this so complex. Why use the (rather finicky) !:N history expansion feature when you already have everything you need passed as an argument? For example:

#! /bin/bash

source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
/usr/bin/rsync -avh -r "$source" "$destination"
rsyncStatus=$?
if($rsyncStatus==0)
then
   cp /Status/Sucesss   /Result/Success_"$folderParam"
else
    cp /Status/Failure   /Result/Failure_"$folderParam"
if

Or, even simpler:

#! /bin/bash

source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
if /usr/bin/rsync -avh -r "$source" "$destination"; then
   cp /Status/Sucesss   /Result/Success_"$folderParam"
else
    cp /Status/Failure   /Result/Failure_"$folderParam"
if

Or even:

#! /bin/bash

source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
touch "/Result/Failure_$folderParam"
/usr/bin/rsync -avh -r "$source" "$destination" && 
    mv /Result/Failure_"$folderParam" /Result/Success_"$folderParam"
3

Terdon's answer is better, but if you really want to use the history operators you can specify which command you want to refer to:

!rsync:3

will be replaced with the third parameter to the last rsync invocation. So in your case,

folderParam=$(basename !rsync:3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.