Use sudo without password INSIDE a script

From my blog: IDMRockstar.com:

The kicker is that sometimes, I need to run commands as root. Here’s the quick and dirty way I accomplish that without divulging the passwords:

#! /bin/bash
read -s -p "Enter Password for sudo: " sudoPW
echo $sudoPW | sudo -S yum update

This way the user is prompted for the password (and hidden from terminal) and then passed into commands as needed, so I’m not running the entire script as root =)

If you have a better, way, I’d love to hear it! I’m not a shell scripting expert by any means.

Cheers!

.: Adam

Leave a Comment