0

Sounds like quite a convoluted question, but it's really not. I'm busy building a menu in a bash script, and as part of the case statement I am trying to call another script that I created a while ago which edits a file, and it's breaking. Here is an example:

#!/bin/bash

PS3="Please choose an option: "
options=("Do this" "Do nothing")
select opt in "${options[@]}"
do 
   case $opt in
      "Do this")
         read -p "Please enter the file name:`echo $'\nE9> '`" FILENAME
         getscript $FILENAME
         break
         ;;
      "Do nothing")
         break
         ;;
   esac
done

The error that I get from this is as follows: getscript: command not found

To illustrate that this works, I ran getscript from the normal command prompt and it still works:

$ getscript randomfilename.txt
done

A little more detail, in case in matters... The getscript.sh script in question is located in /usr/local/bin and I have an alias created for it in .bashrc in my current user directory, which allows me to call getscript only.

Please help?

3
  • 1
    Aliases aren't present in scripts. Commented Sep 3, 2015 at 12:31
  • You could resolve the problem by specifying the complete path, ie, /usr/local/bin/getscript.sh $FILENAME Commented Sep 3, 2015 at 12:50
  • I have suggested an alternate approach. Aliases its self are not supported in scripts. Please accept my answer if it resolved your issue. Commented Sep 3, 2015 at 14:22

2 Answers 2

1

Biffen was correct, aliases don't seem to work in a script. So all I needed to do was add .sh on the end, and it worked perfectly... To illustrate, in case somebody needs this in the future:

PS3="Please choose an option: "
options=("Do this" "Do nothing")
select opt in "${options[@]}"
do 
   case $opt in
      "Do this")
         read -p "Please enter the file name:`echo $'\nE9> '`" FILENAME
         getscript.sh $FILENAME
         break
         ;;
      "Do nothing")
         break
         ;;
   esac
done

Works like a charm :). Thanks guys

Sign up to request clarification or add additional context in comments.

Comments

0

Add these lines into end of ~/.bashrc

function getscript() {
   /usr/local/bin/getscript.sh "$@"

}
export -f getscript

After this

$ source ~/.bashrc

This will fix the issue. If you do not have source package then you need to install it.

3 Comments

It's quite fine to run it from some other directory. The problem is trying to use an alias in a script.
Updated answer not its working with an alternate approach.
Not a very portable ‘solution’.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.