0

How should we copy an executing script from one folder to the another?

The script content is:

#!/bin/sh

zenity --forms --title="Add Friend" \
    --text="Enter information about your friend." \
    --separator="," \
    --add-entry="First Name" \
    --add-entry="Family Name" \
    --add-entry="Email" \
    --add-calendar="Birthday" >> addr.csv

case $? in
    0)
        echo "Friend added.";;
    1)
        echo "No friend added."
    ;;
    -1)
        echo "An unexpected error has occurred."
    ;;
esac

Lets say i have saved the above script as test.sh in home/user/Documents/sh/ and when I execute test.sh by double click it should copy itself to the meow folder home/user/wow/meow/.

1
  • 2
    Add a line at the end of script? cp /path/to/source /path/to/destination
    – Ron
    Jun 15, 2015 at 10:27

2 Answers 2

6

I'm not sure why you'd want to do this, but you can handle this slightly more elegantly than a raw copy.

mkdir -p $HOME/wow/meow
cp "$(readlink -f $0)" "$HOME/wow/meow"
  • It's using $HOME so we aren't reliant on a hard-coded path (moderately more reliable than the ~ substitution for scripting).
  • We check that the target directory exists (and create it if not).
  • We get the actual script location rather than relying on a hard-coded path.
0
2

Add a line in the script to copy itself:

cp /home/user/Documents/sh/test.sh home/user/wow/meow/
1
  • 1
    yeah, like this I mean..:)
    – Ron
    Jun 15, 2015 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .