For some intellectual curiosity and to fix a bug, I want to create a three word alias in bash from a .sh file that then gets copied and sourced in my bashsrc.
My goal was that :
- if a user type in the terminal
rails test test/
the terminal actually executesrails test
- if a user types in the terminal
rails test test/controllers/file1.rb
, the alias I create does not impact this and the terminal actually executerails test test/controllers/file1.rb
It was inspired by
- multi word alias - https://unix.stackexchange.com/a/670978 (but only use 2 words alias while i need three words alias)
- declare bash function in dockerfile - https://stackoverflow.com/a/72879949
aliasRailsTest.sh
rails() {
if [ "$1" = "test" ] && [ "$2" = "test/" ] ; then
shift
rails test "$@"
else
command rails "$@"
fi
}
Dockerfile
COPY .devcontainer/aliasRailsTest.sh /home/$USERNAME/aliasRailsTest.sh
# source the make.sh script in the .bashrc
RUN echo "source /home/$USERNAME/aliasRailsTest.sh" >> "/home/$USERNAME/.bashrc"
It does not work. I tried removing the line 'shift' on the aliasRailsTest.sh
file but it did not work either.
I know for sure the Dockerfile works because I checked bashsrc and my script was there, and also when I started the .sh file was only targeting 2 words alias and it used to work, so I know this part is OK. The issue very likely comes from the .sh file because I moved to 3 words alias.
Thanks for any help.
CodePudding user response:
You also need to check length of argument in your function. You can use:
rails() {
if [[ $# = 2 && $1 = test && $2 = test/ ]]; then
command rails "$1"
else
command rails "$@"
fi
}