3

There are already similar questions but I'm posting this because none of the answers worked. I'm writing a bash script to automate the installation of pacman and AUR packages on my Arch system. The idea is to read a file (myfile) with the package names first line by line and then word by word and perform the installation for each word. This works fine for pacman but not for the AUR Helper (aurman). The part for aurman looks like this:

while read line; do
if [[ "$line" =~ \$[[:space:]]aurman[[:space:]]-S[[:space:]][[:alnum:]]* ]]
then
    aurline=$(echo "$line" | awk '{ $1=""; $2=""; $3=""; print}' | sed 's/^ *//')
    for aurpkg in $aurline
    do
       sudo -u "${my_user}" bash << EOF
aurman -S --noconfirm --needed --noedit "$aurpkg"
wait
EOF
    done
fi
done < "$myfile"

With the options --noconfirm --needed --noedit aurman does not prompt me for Yes/No but for some packages it prompts me for a number. So the problem is in this case the script does not wait, the package does not get installed and aurman produces an error "EOFError: EOF when reading a line". I have tried to pause the script like this:

aurman ...
wait

or like this:

aurman ... &
wait

but none of these works.

So how can I pause my script when aurman prompts me for a number? What is the general approach in cases like this? How could I give an answer for a specific package right from the start when I run the script (e.g. 1 for package x)?

5

The basic problem is that stdin (which aurman is trying to read from) isn't coming from the user, it's being redirected first from $myfile, and then from a here-document containing the shell commands for sudo to run. One option is to pass those files via a different file descriptor, like #3 (which is normally unused). I think you can also simplify it by eliminating the shell run under sudo -- since you're running aurman in the foreground, there's no need to wait for it, so you don't need the shell (and hence don't need the here-doc).

while read line <&3; do
    if [[ "$line" =~ \$[[:space:]]aurman[[:space:]]-S[[:space:]][[:alnum:]]* ]]
    then
        aurline=$(echo "$line" | awk '{ $1=""; $2=""; $3=""; print}' | sed 's/^ *//')
        for aurpkg in $aurline
        do
            sudo -u "${my_user}" aurman -S --noconfirm --needed --noedit "$aurpkg"
        done
    fi
done 3< "$myfile"

If that doesn't work and you actually need the shell run under sudo, you can redirect it via FD #3 as well, and have bash read that as a script, like this:

            sudo -u "${my_user}" bash /dev/fd/3 3<< EOF
aurman -S --noconfirm --needed --noedit "$aurpkg"
wait
EOF
0

For bash, there is the sleep command, that puts the script to sleep until an amount of time in seconds has passed. However, if what you want is to catch a password, I would recommend you rather, one of the 2 methods below(check the script I put at the bottom, to illustrate how sleep works, and also the 2 alternatives mentioned below).

  • You can send the password as a parameter when starting the script.
  • You can use read command, this will record the inputs until the customer press enter.

[root@client ~]# cat readPass.sh
#!/bin/bash

# Author: @djcerdas
password="$1"

# Sample sleep command
echo "Hi, I am the PID $$, I am going to sleep 3 seconds"
date&&sleep 3&&date
echo "---------------------------------------"
# Sample method 1: passing password a parameter
echo  "Method 1: The password is $password"
password=""
echo "---------------------------------------"
# Sample method 2: using read
echo "Method 2: Please provide your password:"
read password
echo The password is $password

[root@client ~]# ./readPass.sh myPasswordX
Hi, I am the PID 2257, I am going to sleep 3 seconds
Tue Apr  3 01:17:55 CST 2018
Tue Apr  3 01:17:58 CST 2018
---------------------------------------
Method 1: The password is myPasswordX
---------------------------------------
Method 2: Please provide your password:
myNewPassword
The password is myNewPassword
[root@client ~]#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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