LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-12-2010, 01:43 AM   #1
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Rep: Reputation: 8
Question Any way to know beforehand if SUDO is (going to be) needed?


I'm using virtual file-system in /proc/ to print out current working directory (CWD):
Code:
ls /proc/$PID/cwd # part of an argument to printf (full printf below)
Now, some processes complain about SUDO privileges (e.g. init: ls: cannot access /proc/1/cwd: Permission denied).
Is it possible to know beforehand if action/command will require SUDO? For example, is there a "maximum PID number", above which no process needs SUDO? Is the following snippet (that checks if user ran the script as SUDO) good enough for behorand-knowledge?
Code:
if [ $UID -eq 0 ] ; then
# do something



BTW, in following printf snippet with 2 arguments,
Code:
printf "%15s  %s\n"  CWD:  $((ls /proc/$PID/cwd) | tr '\n' ' ')
if ls returns error cannot open directory /proc/1/fd: Permission denied it outputs this error before "CWD:", like so:
Code:
ls: cannot access /proc/1/cwd: Permission denied
            CWD:
Why this happens?
 
Old 12-12-2010, 05:02 AM   #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've asked two questions. I will address the first one.

Security settings in Linux are often tweaked by each distribution. Therefore when you are looking around the guts of the system you may have a command work on one distribution and fail on another due to security.

You can know in advance if some information in /proc is available by performing an "ls -l" to view the permission settings on the object that you want to access.

Regarding your second question I believe that you get the error on the second argument before the first argument is printed because you have specified a format string. I believe that the printf function in bash, which is different from the printf external command, tries to format the entire output before it prints the first result. So printf evaluates CWD and succeeds, then it evaluates the second argument and fails and gives an error, then it proceeds to print whatever was successfully processed.

You can find more information using the man pages.
Code:
man bash
Once you are in the man page for bash you can look for the printf command.

But, I could be wrong. I'm no cli expert.
 
1 members found this post helpful.
Old 12-12-2010, 06:24 AM   #3
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by stress_junkie View Post
You can know in advance if some information in /proc is available by performing an "ls -l" to view the permission settings on the object that you want to access.
Indeed, I could use "ls -l" and then check if ( PID's folder owner != $USER ): if yes, then go ahead; if no, print error.

How do I use grep on "ls -l". For example, to find init #1 folder, this works:
Code:
ls | grep ^1$

EDIT: I get it, the last two CODEs don't return anything because grep operates on the whole "row" from "ls -l" (access modifiers, owner, etc.). Now, how can do you cut from "ls -l" only folder's name?


EDIT2: OMG, this works (prints folder names), but this surely isn't the "right way" to do it:
Code:
ls -l | cut -d' ' -f35
Why doesn't the default delimiter (TAB) work with "ls -l" output?!

Last edited by courteous; 12-12-2010 at 06:42 AM.
 
Old 12-12-2010, 07:27 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
An easy way to retrieve directory ownership would be 'stat -c %u /proc/$PID/cwd'. The easiest way to resolve a symbolic link would be to use 'readlink -f /proc/$PID/cwd'. Combining that you could
'[ `stat -c %u /proc/$PID/cwd` -eq $UID ] && { DISPLAY_CWD=$(readlink -f /proc/$PID/cwd); echo "CWD: ${DISPLAY_CWD:=noneofyourbusiness}"; };'.
 
1 members found this post helpful.
Old 12-12-2010, 11:53 PM   #5
rob.rice
Senior Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 1,076

Rep: Reputation: 205Reputation: 205Reputation: 205
MAN UP !!
you don't need no stinking sudo !
just log in as root
 
  


Reply



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
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
Script needed to get users with sudo access mikeb75 Linux - Server 1 12-01-2009 12:21 PM
Sudo Help Needed, Please eshaaf Linux - Newbie 4 11-11-2007 07:15 AM
startup program: is sudo needed? Patrick K Ubuntu 2 04-24-2007 10:29 AM
Restricting Editing in Sudo (Advanced Sudo Question) LinuxGeek Linux - Software 4 11-04-2006 03:20 PM

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

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