Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  130] [ 0]  / answers: 1 / hits: 334901  / 2 Years ago, mon, october 10, 2022, 7:09:48

I have an executable file mpiexec, whose full path is ~/petsc-3.2-p6/petsc-arch/bin/mpiexec. Since I want to execute this command in different directories (without having to retype the entire path), I setup an alias in my home .bashrc file:



alias petsc="~/petsc-3.2-p6/petsc-arch/bin/mpiexec"  


which allows me to execute this mpiexec file at the command prompt easily by typing:



petsc myexecutable


I tried to write a shell script file, named script, using my new alias petsc as a command. After giving my shell script the appropriate permissions (using chmod), I tried to run the script. However, it gave me the following error:



./script: line 1: petsc: command not found


I know that I could just write the full path to the mpiexec file, but it is cumbersome to write the full path everytime that I want to write a new script. Is there a way that I can use my alias petsc inside the script file? Is there a way I can edit my .bashrc or .bash_profile to make this happen?


More From » bash

 Answers
0

Some options:




  1. In your shell script use the full path rather then an alias.


  2. In your shell script, set a variable, different syntax



    petsc='/home/your_user/petsc-3.2-p6/petsc-arch/bin/mpiexec'

    $petsc myexecutable

  3. Use a function in your script. Probably better if petsc is complex



    function petsc () {
    command 1
    command 2
    }

    petsc myexecutable

  4. Source your aliases



    shopt -s expand_aliases
    source /home/your_user/.bashrc



You probably do not want to source your .bashrc, so IMO one of the first 3 would be better.


[#40733] Tuesday, October 11, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deringach

Total Points: 412
Total Questions: 107
Total Answers: 101

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;