1

below is a script that displays a menu, when I run it like ./menu.sh it works, but when I run it like this: ./menu.sh | bash

I get a lot of commands not found.

The reason I want to run it with | bash is because it will be on an ftp server and I need to run it from different remote locations. so I just do a curl -s to the ftp and if i do not use the | bash I just cat a printout of the code

    #!/bin/bash

#$path=./


while :
do
    echo " "
    echo " "
    echo "===== MAIN - MENU ====="


    echo "1. Run old sanity script"
    echo "2. Asterisk"
    echo "3. Audio"
    echo "4. CoCo"
    echo "5. Database"
    echo "6. Mobile"
    echo "7. SSH"
    echo "8. Touch"
    echo "9. VDS"
    echo "10. Wireshark"


    echo -----------------------
    echo "q. Quit"
    echo "r. Reload"
    echo "======================="

    echo -n "Please enter your choice: "
    read opt;
    echo " "
    echo " "

case $opt in


    1)  curl -s sanity_check.sh | bash;;
    2)  exec ./menu/asterisk/menu.sh;;
    3)  exec ./menu/audio/menu.sh;;
    4)  exec ./menu/coco/menu.sh;;
    5)  exec ./menu/database/menu.sh;;
    6)  exec ./menu/mobile/menu.sh;;
    7)  exec ./menu/ssh/menu.sh;;
    8)  exec ./menu/touch/menu.sh;;
    9)  exec ./menu/vds/menu.sh;;
    10)  exec ./menu/wireshark/menu.sh;;


    q)  echo " "
            echo "Bye Bye";
                exit 1;;
    Q)  echo " "
            echo "Bye Bye";
                exit 1;;

    r)  exec ./menu.sh;;
    R)  exec ./menu.sh;;

    *)  echo "$opt is an invaild option.";
            echo "Press [enter] key to continue. . .";
                read enterKey;;
esac
done

it gives me the following errors:

bash: line 3: =====: command not found
bash: line 4: 1.: command not found
bash: line 5: 2.: command not found
bash: line 6: 3.: command not found
bash: line 7: 4.: command not found
bash: line 8: 5.: command not found
bash: line 9: 6.: command not found
bash: line 10: 7.: command not found
bash: line 11: 8.: command not found
bash: line 12: 9.: command not found
bash: line 13: 10.: command not found
bash: line 14: -----------------------: command not found
bash: line 15: q.: command not found
bash: line 16: r.: command not found
bash: line 17: =======================: command not found

but for me these are just the echo commands ..... or am I missing something

2

When running this code, it will output a text menu. What you are doing with ./menu.sh | bash is that you are feeding the menu into bash for execution. In other words, you say to bash, "please execute this menu". bash does not know what any of the text in the menu means, so it throws errors.

However, this is not how you say you will use this script. You intend to fetch it (the script, not its output) and have it execute:

curl -o- 'ftp://.../menu.sh' | bash

This would not execute the script on the left hand side of the pipeline, while your ./menu.sh | bash does. So what you are proposing would probably work.


Also note that it's a fair bit easier to do multi-line output with a here-document:

cat <<END_MENU
===== MAIN - MENU =====
1. Run old sanity script
2. Asterisk
3. Audio
4. CoCo
5. Database
6. Mobile
7. SSH
8. Touch
9. VDS
10. Wireshark
-----------------------
q. Quit
r. Reload
=======================
END_MENU

Also investigate the select statement in bash. It displays menus for you and makes it easier to do user input (unless you want special formatting of the menu and options, obviously).

You can also do multiple patterns in case statements:

q|Q)  echo 'Bye!'
      exit ;;

or

[qQ]) echo 'Bye!'
      exit ;;

(an exit due to the user's request should probably not return a non-zero exit code, by the way)

  • thanks for the advice, the curl -o- option does not fully work, i still get a few errors... i am currently looking in to the select option, but again here, when i want to run it form the remote server using the | bash statement at the end i get errors. – WingZero Dec 19 '18 at 9:46
  • @WingZero huh? What error do you get? – Kusalananda Dec 19 '18 at 9:49
  • 'bash: line 2: $'\r': command not found bash: line 4: $'\r': command not found bash: line 5: $'\r': command not found bash: line 35: syntax error near unexpected token $'in\r'' 'ash: line 35: case $opt in' – WingZero Dec 19 '18 at 9:55
  • @WingZero This is because your script is a DOS text file. DOS text files has an extra carriage return character at the end of each line. Are you writing it on Windows? – Kusalananda Dec 19 '18 at 9:57
  • Hi,maybe it is easier to get the whole story: I've been a system admin for around 15 years now, always worked in windows environments. due to a job change one and a half years ago, i started working on linux systems. i am running ubuntu 18.10 atm, that is where i wrote my scripts (in visual studio code) i have a lot of scripts that i need to run on different systems that are in different locations. – WingZero Dec 19 '18 at 10:21
0

You are sending the output of the script to bash. don't you want to send the text of the script to bash to make bash execute it?

cat menu.sh | bash
  • hi, these scripts are on an ftp server. is there a way to cat with username and password? – WingZero Dec 19 '18 at 15:16
  • so i am still having issues with the |bash command at the end, i cant jus do an echo ... – WingZero Dec 19 '18 at 15:18
  • do you have an idea on how to do en echo command together with the bash command? – WingZero Dec 19 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.