2

When I run a bash script from terminal or nautilus, it works properly, but when I run it via gnome's 'Main Menu', it returns an error.

read -p "" -n1 selection

This is the line, that return the following error:

/usr/local/bin/php-version.sh: 9: read: Illegal option -n

But when I run the script via terminal (./file.sh), there's no problem with this

The Main Menu entry is bash usr/local/bin/php-version.sh

File permissions are 755, owner is root.

EDIT:

/usr/local/bin/php-version.sh

#!/bin/bash

sudo echo -e "sudo/root permission: \e[32mOkay\e[0m"

echo -e "Type \e[2m5\e[0m to change from php version \e[1m7 \e[0mto \e[1m5\e[0m"
echo -e "Type \e[2m7\e[0m to change from php version \e[1m5 \e[0mto \e[1m7\e[0m"

echo -en "\e[1mSelect: \e[0m"
read -p "" -n1 selection
echo

case "$selection" in
    "5") 
        sudo a2dismod php7.0 >> /dev/null
        sudo a2enmod php5 >> /dev/null
        sudo a2query -m php5
        ;;
    "7")
        sudo a2dismod php5 >> /dev/null
        sudo a2enmod php7.0 >> /dev/null
        sudo a2query -m php7.0
        ;;
    *)
        sudo echo -e "\e[91mInvalid input: \e[39m$selection\e[0m"
        echo -en "\e[2mPress Enter..."; sed -n q </dev/tty
        exit 1
        ;;
esac

echo "Restarting apache2..."; sudo service apache2 restart    
echo -en "\e[2mPress Enter..."; sed -n q </dev/tty

/usr/local/bin/test.sh (where the read command always works)

#!/bin/bash

ps aux | grep $$ | grep -v grep

read -p "" -n1 test

echo -e "\n"

env

$SHELL
  • 2
    Could you write a simple debugging script test.sh to check whether gnome does really start a non-posix bash where read -n should work? test.sh could do env to see all variables and ps aux | grep $$ | grep -v grep to see which shell is used. – rudimeier Oct 7 '16 at 21:51
  • ps aux | grep $$ | grep -v grep returns scriptim 3669 0.0 0.0 13236 2844 pts/1 Ss+ 00:08 0:00 /bin/bash /usr/local/bin/test.sh (terminal) scriptim 5188 0.0 0.0 13236 2852 pts/1 Ss+ 00:15 0:00 bash /usr/local/bin/test.sh (Main Menu) POSIXLY_CORRECT is not defined – Scriptim Oct 7 '16 at 22:19
  • @rudimeier yes, both terminal and main menu – Scriptim Oct 7 '16 at 22:28
  • So the problem must be located inside php-version.sh. Could you post both the working test.sh and the bad php-version.sh? (Edit your question.) – rudimeier Oct 7 '16 at 22:32
0

I don't really see why read "" -n1 selection works in terminal and not from the menu. The error message seems misleading too. However the correct bash syntax should be

read -n 1 selection

You may change that line in php-version.sh.

Beside that the whole scripts looks a bit strange. Most of the sudo commands should not be sudo. Anyways that wasn't the question.

  • Fixed the sudo thing, my fault - this had different reasons – Scriptim Oct 7 '16 at 22:57

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.