How to use SU inside script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use SU inside script?
# 1  
Old 03-14-2013
How to use SU inside script?

hi guys,

I new to unix im generating a script within c code.
I need to upgrade the software to a newer version and in need su privilege.
what is the correct syntax in running su within the script?

i would greatly appreciate your help
many thanks
rafael

Code:
/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.03  */

/* Standard headers */
#include <stdio.h>
#include <unistd.h>

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>

/* Local headers */
#include "ablibs.h"
#include "AppGlobal.h"
#include "abimport.h"
#include "proto.h"
#include "version.h"

#include <sys/stat.h>
#include <sys/reboot.h>

struct stat sb;

using namespace std;

int
Yes( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	// find where abc/123/version x is located
	system("find /fs/*/abc/123/*/ > /root/find.txt");

	// only read the first result
	std::string line;
	std::ifstream stream("/root/find.txt");
	getline(stream,line);

	// current version
	int maj = VERSION_MAJOR;
	int min = VERSION_MINOR;
	int pat = VERSION_PATCH;

	// comparing to
	int maj_comp = 0;
	int min_comp = 0;
	int pat_comp = 0;

	// check if it is a valid folder
	if (stat(line.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
	{
		unsigned found = line.find_last_of("/");

		stringstream line_to_int;
		line_to_int << line[found-5];
		line_to_int >> maj_comp;

		line_to_int.str("");
		line_to_int << line[found-3];
		line_to_int >> min_comp;

		line_to_int.str("");
		line_to_int << line[found-1];
		line_to_int >> pat_comp;

		//if ((maj_comp == maj) || ( min_comp == min) || ( pat_comp == pat))
		if ((maj_comp != maj) || ( min_comp != min) || ( pat_comp != pat))
		{
			std::ofstream installation("/root/upgrade.sh");

			if (installation.is_open())
			{
				installation  << "#!/bin/sh\n";
				//installation  << "su -c \n";
				installation  << "cd " << line <<"\n";
				installation  << "sh ./install\n";
				//installation  << "shutdown";
				installation.close();
			}

			system("chmod 777 /root/upgrade.sh");
			system("/root/upgrade.sh");

		}
		else
		{
			cout << "current version is up to date" << endl;
		}

	}

	// check path if it exist if it does check
	// if current version is greater than upgrade
	// perform upgrade

	// else display error message cannot find file
	// check if usb is properly connected
	// check if path is valid


	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	return( Pt_CONTINUE );

}

# 2  
Old 03-14-2013
'su -c' does not read the lines after it, and expects a parameter.

Code:
su -c "cd whatever ; sh ./installation"

Also, su is going to ask for a password, so if this isn't run from a terminal, it won't work. (there are graphical su replacements, like kdesu.)

Also, 777 is not the magic sledgehammer to fix all permissions problems. If this script is to be run by root, 700 should be more than sufficient.

Also, I think you could replace 'find' with 'echo' here for a big performance improvement, since you don't care about finding the contents of those folders (find is recursive, it will locate everything inside /a/*/b.). The shell is the thing which handles these *'s here, not find.

Also, you dont' need to save into a temp file, use popen and read the string directly out of it.

Perhaps something like:

Code:
char buf[1024], *b;
FILE *p=popen("echo /fs/*/abc/123/*/", "r");
fgets(buf, 1024, p);
pclose(p);

// Find last /
b=strchr(buf, '/');
while(strchr(buf, '/')) b=strchr(b, '/');

// Check for filename like /0.1.2.... and read into ..._comp variables
if(sscanf(b, "/%d.%d.%d", &maj_comp, &min_comp, &patch_comp) == 3)
{
        ...
}


Last edited by Corona688; 03-14-2013 at 02:09 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-15-2013
the pipe helped.

i had permission issues on my first create file.
my code works now.

Thanks
This User Gave Thanks to rafae11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

2. Shell Programming and Scripting

How to merge Expect script inside shell script?

Hi I have two scripts one is Expect and other is shell. I want to merge Expect code in to Shell script so that i can run it using only one script. Can somebody help me out ? Order to execute: Run Expect_install.sh first and then when installation completes run runTests.sh shell script. ... (1 Reply)
Discussion started by: ashish_neekhra
1 Replies

3. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

4. Shell Programming and Scripting

Problem in calling a script inside a script

Hi team, I have a script in different folder. Now i want to call that script and execute that script from that path alone. My code is #!/bin/bash wname=yahoo PATH='/opt/IBM' wac=`/usr/bin/ls $PATH | /usr/bin/grep "$wname"` STOP=`/usr/bin/find $PATH/$wac -type f -name "stop.sh"`... (8 Replies)
Discussion started by: natraj005
8 Replies

5. Shell Programming and Scripting

Running a script from if block inside another script

how do i run a script from if block inside another script? this is what i tried but it doesnt seem to work: if test $a -eq $w then sh /home/scripts/script1.bash fi (3 Replies)
Discussion started by: shishirkotkar
3 Replies

6. Shell Programming and Scripting

Running a unix script(which is calling another script inside that) in background

Hi all, I am having a script ScriptA which is calling a script ScriptB in the same server and copying files to second server and have to execute one script ScriptC in the second server. THis First script ScriptA is the main script and i have to execute this process continously. for Keeping... (2 Replies)
Discussion started by: rohithji
2 Replies

7. Shell Programming and Scripting

how to run shell script inside expect script?

I have the code like this : shell script continues ... .... expect -c" spawn telnet $ip expect "login:" send \"$usrname\r\" expect "Password:" send \"$passwd\r\" expect "*\>" send \"$cmdstr\r\" ... (1 Reply)
Discussion started by: robbiezr
1 Replies

8. UNIX for Dummies Questions & Answers

Exit out of the Script Command inside a Script

I'm new to Linux. I have a bash script that invokes an executable. I'd like use the SCRIPT command inside the script and exit out of the script command after it writes to the file. Does this make sense? Below is an example of the contents of my script. #BEGIN SCRIPT script typescript... (6 Replies)
Discussion started by: jmungai
6 Replies

9. Shell Programming and Scripting

Call a perl script inside a shell script

Hi all, I have the following snippet of code.. #!/bin/sh echo "run perl script............" #Run the verification script perl bill_ver echo " perl script completed....." echo "rename files......" #Remove from all file in the directories test, test1, test2, test3 for f in... (3 Replies)
Discussion started by: chriss_58
3 Replies

10. Shell Programming and Scripting

how to find Script file location inside script

I have to find out the file system location of the script file inside script. for example a script "abc.sh" placed anywhere in the file system when executed shold tell by itself the location of it. example #pwd / #./abc this is / #cd /root #./abc this is /root #cd / #/root/abc this... (10 Replies)
Discussion started by: asami
10 Replies
Login or Register to Ask a Question