LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to execute .sh file from different directory though a c++ Program (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-execute-sh-file-from-different-directory-though-a-c-program-4175414177/)

unkn(0)wn 06-30-2012 12:10 PM

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.

Snark1994 07-01-2012 05:26 AM

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?

Nermal 07-01-2012 05:42 AM

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>

bsat 07-01-2012 08:06 AM

for execl to able to execute a script it requires #!/bin/bash as the first line.

unkn(0)wn 07-01-2012 11:58 PM

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!

Snark1994 07-03-2012 05:29 AM

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++.

unkn(0)wn 07-03-2012 02:15 PM

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..

Snark1994 07-04-2012 04:55 AM

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)


All times are GMT -5. The time now is 05:59 AM.