0

I am having some issue in writing a simple executable .sh file via bash.

The order of operation as soon as I open a terminal (ctrl+alt+T) are:

pc:~$ roscd
pc:~/catkin_docking_ws/devel$ cd ..
pc:~/catkin_docking_ws$ cd devel/lib/tuginterface/
pc:~/catkin_docking_ws/devel/lib/tuginterface$ ./tuginterface

I have been investigating this small issue and came across this source which advises to change and rename the project as an alias and that is exactly what I tried to do:

alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"

My current executable file after many trials is:

#!/bin/bash

alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"

But it still does not work.

The same post advises to create a script and after that an alias in the startup file.

Please advise on how to solve this problem and sorry if it is a simple question but I don't seem to catch the mistake I am making.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • Am I correct in understanding you want to be able to run a single command to start the `roscd` command, change directory and run the `tuginterface` program? – Simon Doppler Jun 05 '20 at 12:56
  • You want to execute this script so that it has the effect of creating 4 aliases in the shell that you are executing the script? If so, then you need to execute `. ./tuginterface` instead of `./tuginterface`. – Jeff Holt Jun 05 '20 at 12:57
  • @Simon Doppler, hello and thanks for reading the question. Yes you are correct. – Emanuele Jun 05 '20 at 12:57
  • Why do you need an executable file for this? What's wrong with defining a function in bashrc and using it instead? – oguz ismail Jun 05 '20 at 12:57
  • @oguzismail, thanks for reading the question. How do I do that? could you please add some code to your answer? :) – Emanuele Jun 05 '20 at 12:59
  • The goal of the exercise I am trying to accomplish is being able to execute the `.sh` file only. I saw from the post I mentioned the possibility of aliases but I am not sure is the right way. I almost never use bash script. – Emanuele Jun 05 '20 at 13:01
  • Basically I am trying to just write an executable instead of writing [all this](https://i.imgur.com/ApDFh9F.png) – Emanuele Jun 05 '20 at 13:04
  • See Kugelman's answer below. What I suggested does not involve a `.sh` file. – oguz ismail Jun 05 '20 at 13:26
  • @Emanuele : I don't understand what your script is supposed to do. It contains only of alias definitions, i.e. it does not actually execute anything, because you don't run the aliases. But even if you would run them, note that (from the bash man page): _Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt_, so you would at least have to enable them with `shopt -s expand_aliases`. But I don't see why you want alias definitions in your script. – user1934428 Jun 08 '20 at 06:25
  • @Emanuele : _Please advise on how to solve this problem_ : Maybe you could start by actually **defining** your problem. You say what you are **doing** right now and that you somehow are not completely happy with this. I don't see any precise definition of what you want to achieve. – user1934428 Jun 08 '20 at 06:27

1 Answers1

1

The script doesn't need to define aliases. Aliases are commands that you can type yourself. The script can just execute the commands directly.

#!/bin/bash

cd ~/catkin_docking_ws/devel/lib/tuginterface
./tuginterface

I've combined the three cd commands into one that jumps straight to the correct directory.

"But I thought cd doesn't work in shell scripts?"

It depends what you're looking for. When a script changes directory it affects later commands in the script, so in that respect it does work. The change of directory is only inside the script, though. The person calling the script won't see the directory change. Their current directory is unaffected.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578