Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am wondering how to implement the following in a bash script.

if UID=1000, continue
else exit
confirm choice (y/n)
share|improve this question

2 Answers 2

up vote 3 down vote accepted
[ $UID -ne 1000 ] && exit     # exit if UID ist not equal 1000
share|improve this answer
1  
This is a good solution too and works for the OP's question, but note that it does not work in sh (not relevant here but relevant in general). –  Jason C Jul 13 at 1:36
1  
:) For all friends of sh and portable code: [ "$(id -u)" != 1000 ] && exit –  Cyrus Jul 13 at 2:55

You could do this:

if [ "$(id -u)" = "1000" ]; then
     do stuff for user 1000
else
     do stuff for other users
fi

However, it would probably be more effective to chown the script to that user and then chmod 700, since it appears from your example that you are attempting to stop execution of the script for other users. This would also give you more control i.e. allowing you to use groups (w/ chmod 750 or whatever) to manage who can perform the action. If only part of your script should be accessible to user 1000 you can separate that part into its own script and apply permissions appropriately.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.