LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2009, 05:14 PM   #1
VauxhallVXR
LQ Newbie
 
Registered: May 2009
Posts: 6

Rep: Reputation: 0
Permission Check Function


So this is just out of sheer curiosity.

I've written a small 'permissions check' function in one of my bash scripts.

Code:
function PermissionsCheck {
	if [ "$(whoami)" != "root" ]; then
		echo "You must re-run the script as root!"
		exit
	fi
}
So the logic works, but if it reaches exit, it closes the terminal. I simply want it to mimic a SIGINT signal and just stop the script and keep it at the prompt. I'm just not entirely sure of the command, exit is probably not the right idea.

Any thoughts?
 
Old 06-06-2009, 05:54 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You could try using return instead of exit.

If you are running this from a desktop icon you can use the icon properties to keep the terminal open when the script stops. I use that to read log files.
 
Old 06-07-2009, 09:40 PM   #3
VauxhallVXR
LQ Newbie
 
Registered: May 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by stress_junkie View Post
You could try using return instead of exit.

If you are running this from a desktop icon you can use the icon properties to keep the terminal open when the script stops. I use that to read log files.
Took a shot at the return thing, no dice.

As far as desktop icons, this is all being run from command line.

I gave kill -9 $$ a shot as the else statement, but no go on that either.

Any other ideas? Is there a way to restart the terminal in the same directory as before?
 
Old 06-08-2009, 02:43 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
How do you run your program?
If you source your script instead of running it the usual way, try this:
Code:
#!/bin/bash

function PermissionsCheck {
    if [ "$(whoami)" != "root" ]; then
        echo "You must re-run the script as root!"
        return 1
    fi
    return 0
}

echo "Main script starts here"
if ! PermissionsCheck; then
    return 1
fi
echo "Passed permission check. Your are root. Good."
 
Old 06-09-2009, 09:01 PM   #5
VauxhallVXR
LQ Newbie
 
Registered: May 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hko View Post
How do you run your program?
If you source your script instead of running it the usual way, try this:
Code:
#!/bin/bash

function PermissionsCheck {
    if [ "$(whoami)" != "root" ]; then
        echo "You must re-run the script as root!"
        return 1
    fi
    return 0
}

echo "Main script starts here"
if ! PermissionsCheck; then
    return 1
fi
echo "Passed permission check. Your are root. Good."
Perfect. Thanks very much!
 
  


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
permission problems about passthru() function in PHP RaySwen Programming 5 04-08-2009 04:08 AM
how to check, in linux, setnetpath function in C exists sunilvadranapu SUSE / openSUSE 1 09-19-2008 01:51 AM
How do I check if the php function file() fails? farmerjoe Programming 6 12-02-2007 09:17 AM
Explanation sought for a check in ip_finish_output2() function Pankaj Srivastava Linux - Networking 0 05-24-2007 08:52 PM
Port check function call mrsharky Linux - Networking 4 04-15-2005 11:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 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