Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Limit bash script to its owner

I have different users in /home. Each user has a start.sh script and a start.conf for configuration. Also start.sh in linked to /bin/nameoftheuser to make typing easier (This adds more complexity to the question).

I want to limit the script to the user that works with it. What I currently do is this:

cd
if [ -f start.conf ]; then
    . start.conf
else
    echo "start.conf not found"
    exit 2
fi

if [ $(whoami) != $SCRIPTUSER ]; then
    echo "You are logged in with $(whoami). Only $SCRIPTUSER is allowed to use this script"
    exit 2
fi

$SCRIPTUSER is defined in start.conf. But when I'm logged in with the wrong user and I want to use start.sh, the wrong configuration file gets included so the second part of the code doesn't catch the mistake.

In the end, each user should limit their start.sh script excluselive to them and others should not be able to call it by accident


One idea could be comparing the owner of $0 to the current user. But when there is a symbolic link in /bin/nameoftheuser, the symbolic link is owned by root.

Including the configuration file and reading what user is allowed to use it and compare $(whoami) to it, does also not satisfy. This is because a different user could call it and include their own start.conf

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Why don't you simply use file permissions?

chmod 700 script.sh

That's it.

over 4 years ago · Santiago Trujillo Report

0

This will find the owner of the symbolic link

owner=$(stat --format '%U' $(readlink -f /bin/nameoftheuser))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!