Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-30-2012, 12:10 PM
|
#1
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Rep: 
|
How to execute .sh file from different directory though a c++ Program
Hello,
Sorry if this is not correct place for posting this but i was trying to write a short c++ code to choose different conky's.
Code:
switch(choice)
{
case 1: system("killall -9 conky");
system("conky -c ~/.conky_config_files/.conky_grey/conkyrc_grey");
break;
case 2: system("killall -9 conky");
system("conky -c ~/.conkycolors/conkyrc");
break;
case 3: system("killall -9 conky");
system("conky -c ~/.conky_config_files/.my_conkyrc/conkyrc");
break;
case 4: system("killall -9 conky");
system("conky -c ~/.conky_config_files/.Conkydidi/conkyrc");
break;
case 5: system("killall -9 conky");
// execl("~/.conky_config_files/.GoldnGrey/conky.sh","conky.sh", NULL);
// system("chmod 777 ~/.conky_config_files/.GoldnGrey/conky.sh");
// system("bash ~/.conky_config_files/.GoldnGrey/conky.sh");
break;
default: cout<<"\nAgain.\n";
goto again;
}
My case 5 is not working, tried almost evrything on internet.
I dont know much bash coding. In this program i was trying to execute conky.sh from any directory.
Also, i want to hide the terminal on which i execute this program.
Any help?!!?!?
PS: i know case 5 is commented.
|
|
|
07-01-2012, 05:26 AM
|
#2
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
Why C++? Why not just use bash?
For closing the terminal, I'm pretty sure you can configure conky to jump to fork or run as a daemon or do some kind of magic so that it will not be a child process of your program, and so you can run 'xterm -e "your_program"' and the terminal will close after it's finished but conky won't... I think it's the "background yes" .conkyrc option, but can't remember offhand.
Finally, what commands are you trying to run in case 5? (i.e. if you were doing it manually, from a terminal, what commands would you want to enter?
Last edited by Snark1994; 07-01-2012 at 05:30 AM.
|
|
|
07-01-2012, 05:42 AM
|
#3
|
Member
Registered: Jan 2009
Distribution: Debian
Posts: 59
Rep:
|
Do you have a
in the top of your script?
The OS needs to understand how to run the shell.
If not try:
Code:
/bin/bash <script to run> <command line parameters>
|
|
|
07-01-2012, 08:06 AM
|
#4
|
Member
Registered: Feb 2009
Posts: 347
Rep:
|
for execl to able to execute a script it requires #!/bin/bash as the first line.
|
|
|
07-01-2012, 11:58 PM
|
#5
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep: 
|
I just wanted to create a simple gui to select from different conky configuration. Can i do that with bash coding?
And in case 5 i only tried to run script "~/.conky_config_files/.GoldnGrey/conky.sh" from different directory, say from Desktop!
|
|
|
07-03-2012, 05:29 AM
|
#6
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
Ah, if you're creating a GUI, then stick with C++. Have you checked it's a) executable (or run "chmod 755 <filename>", assuming you own the file) b) has the "#!/usr/bin/env bash" line at the start, as other people suggested? Then you should just need to have
Code:
system("~/.conky_config_files/.GoldnGrey/conky.sh");
to run it. Also, it may be worth putting some 'echo' code (or even use 'xmessage') in your conky.sh file, so you can check it runs - it is entirely possible that your C++ code is running the file correctly, but for some reason conky isn't starting, in which case the bug squashing needs to be done on that code, not your C++.
|
|
1 members found this post helpful.
|
07-03-2012, 02:15 PM
|
#7
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep: 
|
Snark
Thanks, As i was coding in cpp,adding "#!/usr/bin/env bash" didn't work nor in system(""). But making file executable before executing it, helps.
ie.
Code:
system("chmod +x ~/.conky_config_files/.GoldnGrey/conky.sh");
system("~/.conky_config_files/.GoldnGrey/conky.sh");
Above one works!!!
But how to hide that terminal???
i wish to hide terminal when execution of conkyrc is done..
Last edited by unkn(0)wn; 07-03-2012 at 02:20 PM.
|
|
|
07-04-2012, 04:55 AM
|
#8
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
Hm... Like I said, I'm pretty sure it's conky making that terminal. Look:
Code:
/* ~/test/test.cpp */
#include <cstdlib>
#include <gtk/gtk.h>
static void run_script(GtkWidget *widget, gpointer data){
g_print("Running...\n");
system("~/test/1file.bash");
g_print("...and done.\n");
}
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){
g_print ("Exiting.\n");
return FALSE;
}
static void destroy(GtkWidget *widget, gpointer data){
gtk_main_quit ();
}
int main(int argc, char *argv[]){
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Run script!");
g_signal_connect (button, "clicked", G_CALLBACK (run_script), NULL);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
Code:
#!/usr/bin/env bash
# ~/test/1file.bash
echo Ran file 1
sleep 10
And:
Code:
$ ls -l 1file.bash
-rwxr-xr-x 1 snark users 46 Jul 4 10:41 1file.bash
$ gcc -Wall -g test.cpp -o test `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
$ ./test
This doesn't open a terminal for me, just prints the output to the terminal already running. And if I run the script from my desktop, then no terminal opens. Can we see the contents of the script you're running? (also, you shouldn't need to run chmod every time - only once. So you can remove that line from your code)
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 01:38 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|