Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to write a script that will create a new user on a linux (ubuntu) server. I'm new to script writing, I'm having problems on
how to read variables from the user input and using that variable in other parts of the script
This is sort of what I have, but getting stuck in the execution of it. Thanks.
update_pkgs() {
echo "
----------------------
Prerequisites : Making sure everything is up to date
----------------------
"
# checks is all pkgs are up to date
sudo apt-get update -y
# installing necessary pkgs
sudo apt-get install build-essential libssl-dev -y
}
create_user() {
echo "
----------------------
1. Creating a new user with name `<user>` and gives correct access.
----------------------
"
# [ASK]: How to make <user> a variable I read from STDIN
# and pass it around in the following commands
# add new user with the name of `user`
sudo adduser --ingroup www-data --disabled-password <user>
# copy ssh/ folder from `ubuntu` user to new user
# and gives the right permissions/privileges
sudo cp -R .ssh/ /home/<user>/
sudo chown -R <user>:www-data /home/<user>/.ssh/
}
# 1. asks to run the script
echo "
----------------------
Do You Wish to run this Script ?
----------------------
"
select yn in "Yes" "No" create quit; do
case $yn in
Yes)
update_pkgs();
create_user();
break;;
No) exit;;
create)
read -p "Enter name of user: " user
create_user($user)
quit)
break;;
*)
echo 'Invalid option $REPLY'
esac
done
bash always runs commands in order, unless you tell it to run the command in the background.
You can see how to save the stdin to a variable here.
You should make the question more focused. As regards the request in the title (How to get input from STDIN and save it in a variable in bash), it is too generic too.
The answer depends on what you expect to be given from stdin (One word? One line? More lines?), what do you plan to do with it (read in a "scalar" variable, or in an array?), and other factors.
For instance, executing the following in the terminal is enough to read a line from stdin to a a variable named var:
read var
But you want something more for sure.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have a little problem with the i3 tiling window manager's ~/.i3/config file.
I am trying to set the pm-suspend utility as a key-binding of Mod4 (the windows key) + p (the p char).
I have this bash script called suspendandlock in /usr/bin/:
#!/bin/bash
sudo pm-suspend | i3lock -i /home/antony/unity-desk.png -p default -d -n
I previously modified the /etc/sudoers file with visudo, adding this line:
%users ALL = NOPASSWD: /usr/sbin/pm-suspend
So that no one needs the password to execute pm-suspend utility.
Then I have my i3 config file, where I added this:
bindsym $mod+p exec suspendandlock
Where $mod is the Mod4 key.
The script works fine from gnome-terminal when I type the suspendandlock command -> it suspends the system and blocks the screen as expected.
But when I type Mod4+p from keyboard it only blocks the screen without executing pm-suspend.
Why does it not work?
Does anyone knows where I am wrong?
It looks like OP solved the problem, but for anyone else who needed to fix it (myself included), just run:
sudo usermod -aG users `whoami`
which pm-suspend || sudo apt-get install pm-utils #make sure you have pm-suspend command, install it if you don't
run sudo visudo and add:
%users ALL = NOPASSWD: /usr/sbin/pm-suspend
and then add:
bindsym $mod+p exec "sudo pm-suspend | i3lock"
to ~/.i3/config. Running $mod+p will now lock and then suspend your computer.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I know this is kinda off-topic, but since I've found the code I'm talking about here on SO I hope that someone can help me.
I was dealing with a POSTGRES error and read here: Can not connect to local PostgreSQL an answer, the one from vvdpzz that lead me to run that code in my terminal.
After that, my mac stopped working and since then it doens't reboot, it comes till the apple logo that keeps flashing and switching with a folder with a question mark.
I've checked the code that curl execute and it's the following:
#!/bin/sh
BREW_POSTGRES_DIR=`brew info postgres | awk '{print $1"/bin"}' | grep "/postgresql/"`
LION_POSTGRES_DIR=`which postgres | xargs dirname`
LION_PSQL_DIR=`which psql | xargs dirname`
sudo mkdir -p $LION_POSTGRES_DIR/archive
sudo mkdir -p $LION_PSQL_DIR/archive
for i in `ls $BREW_POSTGRES_DIR`
do
if [ -f $LION_POSTGRES_DIR/$i ]
then
sudo mv $LION_POSTGRES_DIR/$i $LION_POSTGRES_DIR/archive/$i
sudo ln -s $BREW_POSTGRES_DIR/$i $LION_POSTGRES_DIR/$i
fi
if [ -f $LION_PSQL_DIR/$i ]
then
sudo mv $LION_PSQL_DIR/$i $LION_PSQL_DIR/archive/$i
sudo ln -s $BREW_POSTGRES_DIR/$i $LION_PSQL_DIR/$i
fi
done
What happened? I'm not an expert. How can I solve this. I've tried to start in secure mode by pressing the shift key while booting but it doesn't work.
This is a good example of why you shouldn't run scripts without understanding what they do. My guess is that one or both of LION_PSQL_DIR and LION_POSTGRES_DIR came out as empty and the script ended up moving things around in your root directory. You should be able to recover your system by following these steps:
Boot your OS X installation disc by putting it in the drive and holding the C key while booting.
From the Utilities menu, choose Terminal
Run the command cd /Volumes
Run the command ls which should show an entry corresponding to the name of your Mac's hard drive
Run the command cd "<name of your hard drive>"
Run the command ls. If the problem is that things got moved from the root of your drive, there will be an archive entry, if there isn't then stop since this will not fix your problem.
Run the command cd archive to enter the archive directory
Run the command mv * .. to move everything in the archive directory back to the root of your harddrive
Close the terminal
Run Disk Utility from the Utilities menu and use repair permissions on your drive.
Reboot your computer and hopefully it will work right now
Never run random scripts like that again.
The 10th step might not be strictly necessary. I haven't tried to do these steps myself, but they should be pretty close to correct. If you get any errors during them stop and put a comment and I will try and figure out how to proceed.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
What should I type on the Mac OS X terminal to run a script as root?
As in any unix-based environment, you can use the sudo command:
$ sudo script-name
It will ask for your password (your own, not a separate root password).
Or you can access root terminal by typing sudo -s
sudo ./scriptname
In order for sudo to work the way everyone suggest, you need to be in the admin group.
sudo ./scriptname
sudo bash will basically switch you over to running a shell as root, although it's probably best to stay as su as little as possible.