0

I have this piece of rudementary code in my .bash_profile that loads on login, but I can't get it working. Probably some easy fix, but I', staring my self blind on it right now. The code:

# Simple backup when editing files with nano
function bu() {
    read -p "Backup >>"`basename $1`"<< b4 edit [Y/n]?" response
    echo $response
    response=$response${response,,} # tolower
    if [[ $response =~ ^(yes|y| ) ]]; then
        mkdir -p ~/.backup
        #cp -v "$1" ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup
        cp "$1" ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup
        echo ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup >> ~/.backup/bu_log.txt
        nano "$1"
    else
        nano "$1"
    fi
}

And it has an alias nano="bu" so, when i write nano, it should ask me if i want to backup the file first (on yes) or just open it in nano straight away. The only thing that happens now is that it keeps asking the question and looping, newer goes to nano. CentOS is the linux

chepner
  • 497,756
  • 71
  • 530
  • 681
b0red
  • 47
  • 7
  • You're defining a function `bu` which executes `nano`, but you say you are aliasing `nano` to `bu`. Isn't that an infinite loop? – lurker Apr 22 '14 at 17:20
  • Please learn how to format code in SO. I fixed it for you, but then you "unfixed" it. – lurker Apr 22 '14 at 17:21
  • Possibly, but I want it to ask for backup when I type nano SomeFile.sh – b0red Apr 22 '14 at 17:23

3 Answers3

2

Since nano is an alias to bu, typing nano runs your function, which calls nano, which is an alias to bu, which calls nano, ...

In order to run the actual nano editor, you need to disable alias expansion for that call. Use the command built-in:

command nano "$1"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Aliases are so weird. If you define the alias *before* defining the function, you'll get a loop. If you define the alias *after* defining the function, the alias will not be expanded in the function. – kojiro Apr 22 '14 at 17:31
0

I think you want:

response=${response,,}

You have

response=$response${response,,}

which gets you response=Yy. That won't match your regular expression.

You could also just do shopt -s nocasematch.

Aliases are usually trouble. The rule is "if in doubt, use a function."

nano() {
  bu "$@"
}
bu() ( # Use a subshell to avoid having to reset shell options
  shopt -s nocasematch
  local base=${1##*/}
  read -p "Backup >>${base}<< b4 edit [Y/n]?" response
  case $response in
    y*)
      mkdir -p ~/.backup
      local backup=~/.backup/"${base}-$(date +%Y%m%d%H%M`).backup"
      cp "$1" "$backup"
      echo "$backup" >> ~/.backup/bu_log.txt
    ;;
  esac
  command nano "$@" # Use "$@" to allow you to pass more than one argument to nano
)
kojiro
  • 74,557
  • 19
  • 143
  • 201
0

You are calling nano recursivly, since you aliased nano=bu

so try to change the line in the script

nano "$1"

to the full path of

/usr/bin/nano "$1"

(or where nano is installed on your system)

Soren
  • 14,402
  • 4
  • 41
  • 67