LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-30-2012, 12:10 PM   #1
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Rep: Reputation: Disabled
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.
 
Old 07-01-2012, 05:26 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 07-01-2012, 05:42 AM   #3
Nermal
Member
 
Registered: Jan 2009
Distribution: Debian
Posts: 59
Blog Entries: 2

Rep: Reputation: 6
Do you have a
Code:
#!/bin/bash
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>
 
Old 07-01-2012, 08:06 AM   #4
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
for execl to able to execute a script it requires #!/bin/bash as the first line.
 
Old 07-01-2012, 11:58 PM   #5
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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!
 
Old 07-03-2012, 05:29 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
Old 07-03-2012, 02:15 PM   #7
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-04-2012, 04:55 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc will not execute, responds with 'no such file or directory' and 'no input file' nckeecho Ubuntu 9 07-24-2016 01:04 PM
What program is best to monitor a directory and execute a script on a change in dir? Andrew_87 Linux - Software 2 11-02-2010 01:16 PM
execute a program file ernst Linux - Newbie 5 08-29-2008 09:02 PM
how to execute a script file? Have file/directory not found error sirius57 Linux - Software 2 11-21-2007 11:43 PM
Using ./ to execute a file in current directory benchmarkman Linux - General 25 06-18-2007 08:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration