Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-12-2010, 02:43 AM   #1
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):
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?
if [ $UID -eq 0 ] ; then
# do something

BTW, in following printf snippet with 2 arguments,
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:
ls: cannot access /proc/1/cwd: Permission denied
Why this happens?
Old 12-12-2010, 06:02 AM   #2
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
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.
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, 07:24 AM   #3
LQ Newbie
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
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:
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:
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 07:42 AM.
Old 12-12-2010, 08:27 AM   #4
Registered: May 2001
Posts: 29,394
Blog Entries: 55

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
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-13-2010, 12:53 AM   #5
Senior Member
Registered: Apr 2004
Distribution: slack what ever
Posts: 1,036

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


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

All times are GMT -5. The time now is 10:28 PM.

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