Cannot get terminal application to launch with a graphical launcher when successful in terminal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot get terminal application to launch with a graphical launcher when successful in terminal
# 1  
Old 02-15-2016
Cannot get terminal application to launch with a graphical launcher when successful in terminal

I have been having an extremely annoying problem. For the record, I am relatively new at this. I've only been working with unix-based OS's for roughly two years, mostly Xubuntu and some Kali. I am pretty familiar with the BASH language, as that's the default shell for debian. Now, I've made this BASH script that works properly when it is accessed through the virtual console, xfce4-terminal 0.6.3. I've created a symbolic link in /bin to access the script from any directory, as most people do, I think. This is the script itself, designed to connect to WiFi access point from a CLI or virtual terminal.

Code:
 #!/bin/bash

 # WiFi Commander, a lazy way to lazily connect to the WiFi

 # Updated: 2-2-16, version 1.6

 # This script requires that nmcli, w3m, and w3m-img (if
 # you want to be able to see pictures), are all installed
 # on the computer running it


DDG="www.duckduckgo.com"

Cmdr () {
echo; echo "WiFi Command??"; echo
read command
}

test () {
echo; ping -c 1 $DDG
if [ "$?" -eq "0" ];
then echo; echo "Connection Successful."
else echo; echo "Connection Unsuccessful."
fi
}

interweb () {
echo; w3m $DDG
if [ "$?" -eq "0" ];
then echo " ";
else echo; echo "You are not connected to the internet."
fi
}

menu () {
echo; echo "Command Menu:"; echo
echo "S: Scan for WiFi"
echo "CTN: Connect to new WiFi"
echo "C: Connect to a Saved Connection"
echo "T: Test Connection"
echo "D: Disconnect from WiFi"
echo "VC: View Saved Connections"
echo "DelCon: Delete a Saved Connection"
echo "LB: Launch Browser (W3M)"
echo "SM: Show this Menu"
echo "E: Exit"
}

echo; echo "Welcome to WiFi Commander."; sleep .75

menu

while true; do

Cmdr

if [[ "$command" == S ]] || [[ "$command" == s ]];
then echo; echo $(iwlist scan | grep -E "Quality|Signal level|Encryption key|ESSID"; echo);
elif [[ "$command" == CTN ]] || [[ "$command" == ctn ]];
then echo; echo "SSID:"; echo; read ssid; echo; echo "Key:"; echo; read passwd; sudo nmcli d wifi connect "$ssid" password "$passwd" iface wlan0; echo
elif [[ "$command" == C ]] || [[ "$command" == c ]];
then echo; echo $(nmcli c); echo; echo "Connect to??"; echo; read ssid; nmcli c up id "$ssid";
elif [[ "$command" == T ]] || [[ "$command" == t ]];
then test
elif [[ "$command" == D ]] || [[ "$command" == d ]];
then nmcli d disconnect wlan0
elif [[ "$command" == VC ]] || [[ "$command" == vc ]];
then echo; echo $(nmcli c); echo
elif [[ "$command" == DelCon ]] || [[ "$command" == delcon ]];
then echo; echo $(nmcli c); echo; echo "Delete which??"; echo; read ssid; nmcli c delete id "$ssid";
elif [[ "$command" == LB ]] || [[ "$command" == lb ]];
then interweb
elif [[ "$command" == SM ]] || [[ "$command" == sm ]];
then menu
elif [[ "$command" == E ]] || [[ "$command" == e ]];
then echo; echo "Goodbye."; echo; exit
else echo; echo "Error: Command not recognised."
fi

done

This application when summoned via the symbolic link I've created for it, "w-cmdr", runs perfectly. But when I create a graphical launcher, the same kind that functions, say, an Internet browser, or a common application such as LibreOffice, it will not function. It executes the first actual command, which introduces the script and awaits the "command" from the script's "command menu" (as shown in the first picture). But anything actually entered into that menu results in a script failure, from which the only way to exit the script is CTRL+C. The failure is shown in the second picture.

What I don't understand is why it runs when originally executed by the CLI or virtual console, but when summoned by a graphical application launcher, which is configured to bring up a virtual console via the "run in terminal" option, as shown in the third picture, fails with no explanation. I use this script often, even when I'm using the GUI, and having a graphical launcher is honestly, not terribly important. But in enhances my laziness, which is the point of shell scripts haha. So please, someone help me. This is very frustrating. Any suggestions or ideas would be appreciated.
Cannot get terminal application to launch with a graphical launcher when successful in terminal-screenshot_2016-02-14_21-54-29png
Cannot get terminal application to launch with a graphical launcher when successful in terminal-screenshot_2016-02-14_21-56-29png
Cannot get terminal application to launch with a graphical launcher when successful in terminal-screenshot_2016-02-14_21-57-41png
# 2  
Old 02-15-2016
Whatever shell is running your script via the "run in terminal" command is a shell that doesn't know about the recent bash and ksh [[ expression ]] test syntax.

If you convert all occurrences of expressions of the form:
Code:
[[ "$command" == S ]] || [[ "$command" == s ]]

to:
Code:
[ "$command" = S ] || [ "$command" = s ]

or convert the entire if ... elif ... fi tree to a case statement:
Code:
case "$command" in
([Ss]) echo; echo $(iwlist scan | grep -E "Quality|Signal level|Encryption key|ESSID";;
([Cc][Tt][Nn]) echo; echo "SSID:"; echo; read said; echo; echo "Key:"; echo; read passwd; sudo nmcli d wifi connect "$ssid" password "$passwd" iface wlan0; echo;;
...
esac

then most shells based on Bourne shell syntax should be able to run your script.

However, note that naming a function test (overriding a utility normally found in /bin and the built-in found in most shells that accept Bourne shell syntax) is a VERY BAD idea.

Unless all of the utilities invoked by your script are found in /bin, you also need to set PATH to include a list of the directories containing those utilities.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-15-2016
Thank you very much for your time and knowledge, the syntax error of the test statements were the problem. I'm just curious, what is the difference between:

Code:
[[ $variable == A ]]

and

Code:
[ $variable = A ]

I was taught to do the first when I first started to write scripting, and it's never given me any problems until this, which has now been fixed.

---------- Post updated at 03:43 AM ---------- Previous update was at 02:49 AM ----------

Also, using the "case" statement in this really made it way better. And I had never figured out how to properly use it until I saw what you wrote, so again, thank you. Showing me all of this has really improved my script and has given me more knowledge to equip me to write better scripts.

Last edited by Huitzilopochtli; 02-15-2016 at 01:40 PM..
# 4  
Old 02-15-2016
I think the script's shebang #!/bin/bash is malformed, so it doesn't use bash as the interpreter.

Did you consider bash's select statement for your menu?
# 5  
Old 02-15-2016
Could you please explain to me what BASH's "select" is?? I'm not familiar with this concept.
# 6  
Old 02-15-2016
It's an easy way to dynamically build menus from a list of supplied words. c.f. man bash.
# 7  
Old 02-15-2016
Quote:
Originally Posted by Huitzilopochtli
Thank you very much for your time and knowledge, the syntax error of the test statements were the problem. I'm just curious, what is the difference between:

Code:
[[ $variable == A ]]

and

Code:
[ $variable = A ]

I was taught to do the first when I first started to write scripting, and it's never given me any problems until this, which has now been fixed.

---------- Post updated at 03:43 AM ---------- Previous update was at 02:49 AM ----------

Also, using the "case" statement in this really made it way better. And I had never figured out how to properly use it until I saw what you wrote, so again, thank you. Showing me all of this has really improved my script and has given me more knowledge to equip me to write better scripts.
I hadn't noticed that the first line of your script contains a leading <space> character before the #!/bin/bash, so RudiC is probably correct in saying that that is why your script wasn't run using bash as its interpreter. Unless #!interpreter_path starts in column 1 on the 1st line in your file, that line is just a comment and has absolutely no effect on what interpreter will be used to run your script.

You haven't told us what operating system you're using, but it looks like the default shell on your is a Bourne shell or something like dash that doesn't include a lot of the bells and whistles provided by shells like bash and ksh that are extensions above and beyond what is required by the POSIX standards.

In POSIX-conforming shells the command:
Code:
[ expression ]

is a synonym for the command:
Code:
test expression

and there is no specification of what:
Code:
[[ expression ]]

does. The test and [ commands are utilities and they are often built-in into the shell as well as being available as stand-alone utilities. The [[ expression ]] is not a utility; it is a part of the syntax of those shells that provide this extension.

So, using [ "$var" = "string" ] is portable to a wide range of shells. And on shells that support [[ "$var" == "string" ]], it is often a little bit faster, often supports more operators than test, and has different requirements for when double-quotes are required and how shell variables are expanded.

I'm glad my sample case statement helped you figure out how it works. We're here to help you learn how to use the tools that are available to you on UNIX, Linux, and similar systems.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Terminal Output Exactly how it Appears in the Terminal to a New Text File

Hello All, I have a text file containing output from a command that contains lots of escape/control characters that when viewed using vi or view, looks like jibberish. But when viewed using the cat command the output is formatted properly. Is there any way to take the output from the cat... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. UNIX for Dummies Questions & Answers

Routing command to another graphical terminal

So, I'm in a graphical terminal (xfce4-terminal) and I was wondering, would there be a way to type a command, and it run in a new terminal window?? An example would be like, say that I want to open a .txt file, but I want it in a different window, instead of the one that I'm currently using because... (1 Reply)
Discussion started by: Huitzilopochtli
1 Replies

3. UNIX for Dummies Questions & Answers

pbpaste to application in terminal

Is it possible to execute a pbpaste command to an application or current application in focus? Thanks (0 Replies)
Discussion started by: fhill2
0 Replies

4. Red Hat

Not able to see the terminal icon in the applications menu to launch the command prompt in Centos

After installing centos iam not able to see the terminal icon in the applications menu to launch the command prompt in Centos. However iam able to see the Open Terminal menu, when i right click and it is not working. let me know what are the things i need to check.:b: (1 Reply)
Discussion started by: Kesavan
1 Replies

5. OS X (Apple)

Can't launch x11 remotely from terminal

After I installed OS X Lion I haven't been able to launch x11 remotely (using ssh) from Terminal. It works fine locally, and also remotely directly from the Xterm. I log in to the unix server at my university from the terminal like this: ssh -l -X login@host.com This used to launch... (1 Reply)
Discussion started by: gnyrf
1 Replies

6. Shell Programming and Scripting

How do I launch a command on an existing terminal in unix using PERL

Hello, I have a PERL-TK based GUI from which I want to launch a command on an existing UNIX terminal (this is also the parent terminal for this perl based gui window). The command I want to launch is interactive (there is no intention to interact with that command from the same PERL gui i.e. no... (2 Replies)
Discussion started by: AnuragJindal
2 Replies

7. Programming

Redirecting Terminal to Local Application!

i wanted to execute some terminal commands on local linux, parse their output and display it to the user, i checked netcat source code but i couldnt understance it since im new to c (and linux at the same time). so i was wondering if there is away to run an instance of terminal hidden, read and... (15 Replies)
Discussion started by: JonhyM
15 Replies

8. OS X (Apple)

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In the... (2 Replies)
Discussion started by: JFraser1
2 Replies

9. Shell Programming and Scripting

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In... (3 Replies)
Discussion started by: JFraser1
3 Replies

10. Solaris

Script to launch terminal window?

Hi, I am a newbie here. Trying to find a way of writing a script to launch multiple terminal or console windows on solaris 9. I used to be able to do this using cmdtool on older versions of solaris and it was even possible to configure the size and screen position of the window and the title. ... (5 Replies)
Discussion started by: omerta
5 Replies
Login or Register to Ask a Question