need help, trying make a command where the user presses any keys and it will go to the home page, however it doesnt work, why ?
echo -p "press any key to return to main menu" menu
if [[ $menu -gt 1 ]]; then
echo -p "\n"
home
fi
need help, trying make a command where the user presses any keys and it will go to the home page, however it doesnt work, why ?
echo -p "press any key to return to main menu" menu
if [[ $menu -gt 1 ]]; then
echo -p "\n"
home
fi
What system and shell are you using? What error are you getting?
im using unix and the error is that it wont execute it so it just shows the text "press any key to return to main menu" and finishes like that.
It's working as expected. The way it is, there is no variable $menu so the test fails.
Try this:
echo -p "press any key to return to main menu"
read menu
if [[ $menu -gt 1 ]]; then
echo -p "\n"
home
fi
That will blow up when the user just hits enter because menu will be a blank string, evaluating to [[ -gt 1 ]], which is a syntax error.
Try if [[ "$menu" -gt 1 ]]; then
I was focusing on the read command and not the rest. Thanks.
---------- Post updated at 02:04 PM ---------- Previous update was at 01:46 PM ----------
Actually, in the korn shell, the double brackets mean use the shell built-in test and it handles the variable being null, thus the code would not error, it would continue after the end of the if statement. Case in point:
#!/bin/ksh
unset qwaszx
if [[ $qwaszx > 1 ]]; then
print in here
else
print here instead
fi
Output:
$ ./aa
here instead
$
Nonetheless, for clarity and possible portability to other shells I would still quote the variable inside the test as a matter of good practice.
[[IMG]http://linux.unix.com/images/buttons/edit.gif[/IMG]](http://www.unix.com/editpost.php?do=editpost&p=302526439)