1

I am making some service and I have a setup.sh file which must be exectued first time (or on update). This script should create some kind of alias which will be accessible from this or new terminal (also after system restart). This alias should point to another executable sh file.

The setup.sh file:

#!/bin/bash

alias customservice='./customservice.sh "$@"'

As you can see this customservice.sh script is ready to accept multiple arguments. With this customservice alias I want to call this script like this:

customservice //show help
customservice start
customservice stop
customservice exec bash
...

My customservice.sh fil:

if [ $# -eq 1 ] && [ $1 = "start" ]; then
   //do something
elif [ $# -eq 1 ] && [ $1 = "stop" ]; then
   //do something
elif [ $# > 1 ] && [ $1 = "exec" ]; then
   //do stuff with ${@:2}
else
   echo "help"
fi

Maybe customscript kill command would be nice too. It should remove this alias entirely.

Maybe, if this approach is deprecated, I am open for better solutions.

izupet
  • 1,529
  • 5
  • 20
  • 42
  • 2
    What exactly is your question? – Markku K. Dec 18 '15 at 20:29
  • 2
    Why don't you rename the script and done? – Diego Torres Milano Dec 18 '15 at 20:32
  • 1
    That alias doesn't work the way you expect. The `"$@"` in the alias will not expand to the arguments passed to the alias. That's not how aliases work. It will expand to the current positional parameters (usually not set in an interactive shell). Also that alias is sort-of useless. – Etan Reisner Dec 18 '15 at 20:56
  • 1
    That alias saves five characters (from a command which can be tab-completed). Why do you want this alias in the first place? – Etan Reisner Dec 18 '15 at 21:02
  • As i said alias may not be the proper way to achieve what i want, but the scenario remains the same. This sample lines of code are here just to show what i want to achieve. So i want some kind of setup file which set everything up and then some alias which executes another script which manage services establiseh by setup script. – izupet Dec 18 '15 at 21:25

2 Answers2

5

That's not the best way to do what you are aiming for. If you can reconsider your approach I would suggest doing the following:

  1. Use your bash startup file, aka ~/.bashrc ( or similar ) to set the path to script: export PATH=$PATH:/path/to/script/dir
  2. Name your script customservice, not customservice.sh.
  3. Add Shebang to your script, for example #!/usr/bin/sh or #!/bin/bash depending on what you need to execute it and the location of interpreter. Alternatively, for universal approach you can use #!/usr/bin/env bash.

Benefits:

  1. You keep simple things simple.
  2. You do not add an alias layer to your actions - script is called directly.
  3. You don't need to find a way to pass $@ to an alias, which can be dirty.

If you don't want to use the direct approach, Make a Bash alias that takes a parameter? should fully address your needs.

Community
  • 1
  • 1
Zloj
  • 2,235
  • 2
  • 18
  • 28
0

If you are looking to load aliases in ~/.bashrc:

#!/bin/bash 

#add following in your setup.sh 
cat << EOF >> ~/.bashrc
alias customservice='./customservice.sh \"\$@\"'
EOF

on new terminals having bash shell as default it should activate the alias.

obviously, you should grep in your ~/.bashrc with your setup.sh before installing the lines in case the alias is already present.

VK Kashyap
  • 168
  • 1
  • 10