Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

0

Why don't you simply use file permissions?

chmod 700 script.sh

That's it.

over 4 years ago · Santiago Trujillo Relatório

0

This will find the owner of the symbolic link

owner=$(stat --format '%U' $(readlink -f /bin/nameoftheuser))
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda