5

I'm writing a function called restore that will copy a file from a backup directory to the current directory. I now need to create a hard link to restore so that it can be called as purge. How would I implement it so that I could use the if statement if [ "$0" = "purge" ] for when restore is called as purge?

Here is my code, although I will shorten it since I have tested it (it works):

restore(){
if [ "$1" = "-p" ] || [ "$0" = "purge" ]; then
while [ ! ]
    do
    #Purge code, etc...
    done


elif [ "$1" != "-p" ]; then
select fname in $(find /$HOME/Backup -name "$1*" | sed 's!.*/!!' | sed 's!$1!!') quit
        do
             #If restore is called with an argument code...
        done

local newfname=$(echo "$fname"|sed -E 's/[0-9]{11}$//')
cp -i "/$HOME/Backup/$fname" "$newfname"
exit 0
fi

while [ ! ]
do
fname=""
select fname in $(ls /$HOME/Backup) quit
        do
            #Restore with no arguments code...
        done

local newfname=$(echo "$fname"|sed -E 's/[0-9]{11}$//')
cp -i "/$HOME/Backup/$fname" "$newfname"
done
}

Calling restore with the -p option is the same as invoking restore as purge. So how would I implement the code so that restore can be invoked by using purge?

It is supposed to be a script rather than a function. I made a hard link to Restore.sh named Purge.sh, but when I call it using ./Purge.sh it still runs the standard Restore code. How can I determine if Restore is called by the hard link file?

  • $0 wont work as that will just refer to your shell I believe. FUNCNAME is closer to what you need but I just tested it and if I create an alias to my function and call it by the alias, this will still display the function name. – Jesse_b Dec 8 '17 at 22:52
  • 4
    You don't make hard links to a function, you make hard links to a file. A file can contain a compiled binary executable, or ... (what else?) Also, see BashPitfall #1 (Yours has select, the article has for. It doesn't matter.) – ilkkachu Dec 8 '17 at 22:52
  • 1
    It may be best to make this a script rather than function, add it to your PATH and then create a symlink to it in your PATH that is labeled purge. – Jesse_b Dec 8 '17 at 22:55
  • 2
    About your edit: If it is a script and you have made hard links, you need to compare $0 with "./Purge.sh" to make it work, not "Purge". You also need to consider many other possible ways of invoking your script Purge.sh. So generally I won't suggest you compare $0 with anything and use some other workarounds. – Weijun Zhou Dec 8 '17 at 23:40
  • @ilkkachu It can also contain a script. – wizzwizz4 Dec 9 '17 at 8:57
5

Do the hard link to the restore.sh:

ln restore.sh link_to_restore.sh

The content of the restore.sh file:

#!/bin/bash

if [ "$0" = "./link_to_restore.sh" ]; then
    echo foo
elif [ "$0" = "./restore.sh" ]; then
    echo bar
fi

Testing

$ ./restore.sh 
bar
$ ./link_to_restore.sh 
foo
  • 3
    This won't work if I call the script using an absolute path (/path/to/restore.sh) – Emanuel Vintilă Dec 9 '17 at 7:40
  • 2
    You can check whether it actually matches using a regular expression, e.g. *\/restore\.sh. – wizzwizz4 Dec 9 '17 at 8:58
  • it would be better checking if [ "${0##*/}" = "purge" ] i.e. remove all content up to (and including) the last slash, so you compare with only the filename and discard the path in the comparison. – Carlos Campderrós Dec 9 '17 at 15:33
8

Would this be cheating? In your restore script you still have your -p logic and then you create a separate purge function as follows:

purge () { /path/to/restore.sh -p "$@"; }

In your restore script you should take the function wrapper of it out or I believe it will essentially just be sourced and not executed.

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.