You are not logged in.

#1 2022-06-24 13:32:23

alexanderzhirov
Member
Registered: 2022-05-12
Posts: 39
Website

How to properly request data so that variables are saved after request

I am writing a script that will request superuser authorization during execution and continue execution. I have an error when requesting rights, but I can't figure out at what point it happens. The fact is that the TS_DIRECTORY variable resets the previously entered data after requesting sudo rights. I ask for help:

#!/bin/sh

SRC_PATH=$(pwd)/build
PACKAGES=./packages.list
read -ep "Путь расположения корневого каталога: " TS_DIRECTORY
DST_PATH=$TS_DIRECTORY/build

if [ "$(id -nu)" != "root" ]; then
    sudo --reset-timestamp
    read -sp "Для выполнения необходимо ввести пароль суперпользователя: " PASSWORD
    exec sudo --stdin --prompt '' "$0" "$@" <<< "$PASSWORD"
    exit 1
fi

if [ ! -e $TS_DIRECTORY ]; then
    echo "Указанной директории не существует!"
    exit 1
fi

echo $TS_DIRECTORY
echo $DST_PATH

Offline

#2 2022-06-24 13:47:58

seth
Member
Registered: 2012-09-03
Posts: 57,052

Re: How to properly request data so that variables are saved after request

https://man.archlinux.org/man/core/sudo … e-env=list
But you may just want to move the priv elevation to the top and I also don't understand why you read the password into a variable?

Offline

#3 2022-06-24 14:33:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,229
Website

Re: How to properly request data so that variables are saved after request

seth wrote:

I also don't understand why you read the password into a variable?

To create a huge security hole, of course! smile


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2022-06-24 22:22:06

ayekat
Member
Registered: 2011-01-17
Posts: 1,607

Re: How to properly request data so that variables are saved after request

So this reads something into TS_DIRECTORY, then launches itself with sudo (thus prompting the user again for TS_DIRECTORY), no? Or what exactly is said "error"?

By the way: Both `read -p` and `<<<` are not POSIX features, so the `#!/bin/sh` shebang isn't correct (and it would fail if /bin/sh is symlinked against something that doesn't support those).
They look like bash/zsh features, so #!/bin/bash or #!/usr/bin/zsh (or the variant with /usr/bin/env) would be correct.


pkgshackscfgblag

Offline

Board footer

Powered by FluxBB