Automate Script using pkgrm/pkgadd commands

This is my first post so hello to all of you!

I have a script in bash-3.00 that needs to execute the following:

pkgrm [package-name] (to remove an existing package)
pkgadd -d [package-path] ( to install a new package)

The problem is that both commands prompt me to answer either y,n or q in order to proceed with removal/addition 3 times each. If I just run the command I will have to type 'y' and press Enter 3 times (for pkgrm) and type 'all' one time and 'y' two times both followed by Enter (for pkgadd).

I tried to automate this using the following:

echo "Removing existing package..."
pkgrm $PACKAGE << EOF
y
y
y
EOF

echo "Adding new package..."
pkgadd -d $PATH/$PACKAGE.pkg << EOF
all
y
y
EOF

but it's not working. In fact only the first input line works (first 'y' for pkgrm and 'all' for pkgadd) and then the command exits as If I had typed 'q'.

Can anyone help me on this? Is there any way that I can automate this so that I won't have to answer the questions manually?

Thanks,

Insight

Maybe this helps:

man pkgrm

These are the original System V software management tools. Most of the Unix systems understand them. The "-n" you mentioned should be commonplace.

I hope this helps.

bakunin

A native installed Debian Linux or an AIX doesn't. Found it with Solaris man pages though. Thanks for the hint :slight_smile:

I tried using the -n option but this only bypasses the first question which is:

"Do you want to remove this package? [y,n,?,q]"

-n just automates this question and after that I get the second one along with the error message:

"This package contains scripts which will be executed with super-user
permission during the process of removing this package.
Removal of <new_package> was suspended (interaction required)."

and the script terminates.

If I don't use -n then the second prompt is:

"This package contains scripts which will be executed with super-user
permission during the process of removing this package.
Do you want to continue with the removal of this package [y,n,?,q]
Removal of <new_package> failed.
No changes were made to the system." (looks like I typed 'n' or 'q')

Any other suggestions?

Just found this:

Solaris 9 Discussion - How to remove automatically packages?

They are using this answerfile not as a here-script but just as an input file by redirecting it like

pkgrm somesoftware < infile

I checked the link you sent me.

The pkgrm works fine if I use "yes | pkgrm [package-name]" because all the answers should be 'y'.

However pkgadd doesn't work because the first answer should be 'all' and the second 'y'. I tried using an input file as the topic in the link suggests by having:

bash-3.00$ cat input
all
y
bash-3.00$

but it doesn't work. I tried using a combination of " yes | ..." and an input file but it doesn't work as well.

Any ideas?

I think you got it wrong.

You write a simple file with the answers in the order you like them:

cat infile:

y
n
n
y
all
n
y

Then you just call your command like I posted before:

pkgrm somesoftware < infile

This won't work anyway, there is an "echo" missing in front of "yes".

I think you got it wrong :slight_smile:

I need to use both pkgrm and pkgadd. pkgrm works fine if i use
yes | pkgrm $PACKAGE (without echo)

The problem is with the pkgadd because the first answer should be 'all' and the second 'y'. Not all the answers are yes otherwise the above trick would work for pkgadd as well. I also tried using an input file with :

all
y

but it doesn't work. If you read my previous post again you will understand. Any other help?

Sadly I don't have those tools available - yes, maybe someone else will help you, good luck ^^

Just to clarify: "yes" (/usr/bin/yes) is a tool which produces streams of "y^M" (a "y" followed by a newline). AIX has it as any other Unix derivate i am aware of.

I suspect the reason why pkgadd does not work with an input redirection is because it clears the input channel before reading the second input. This is meant as a security device (to avoid answers which in fact are just accidentally doubly typed keys from the last answer) but is in fact puting an obstacle in using the tool in any pipeline or pipeline-like construct. This is plain bad programming style of the i-have-not-understood-Unix-concepts category.

Aside from feeling sympathetically with you i fear there is no solution to this problem. pkgadd is simply programmed badly on your system.

I hope this helps.

bakunin

That "yes" tool I did not know, yep. But I meant that I don't have those pkgadd and pkgrm tools available etc.

The brute-force solution would be to write a simple expect script. It's not a tool I particularly like but it sounds right for this problem.

I finally found the solution to this problem by doing a simple 'trick'.

pkgadd has an -a option which allows to use your own admin file to control the process. The format of this file is similar to the one below.

instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default

The 'action=nocheck' line tells to the command to bypass the questions in order to automate the execution.

The problem is that the first question which needs an 'all' answer (instead of 'y') cannot be bypassed by this command.

What I did was to create two files. The first one had the above 'admin file' structure and the second one had only one line with the word 'all'.

echo "mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default" > $NOASK_FILE

echo "all" > $INPUT_FILE

By using the following command:

pkgadd -a $NOASK_FILE -d [package-path] < $INPUT_FILE

the package was added automatically without any user interaction.

I hope this helps others that will face the same issue in the future.

Thanks for your replies.

1 Like