Solved

Shell scripts

Posted on 1998-09-16
10
398 Views
Last Modified: 2010-04-22
How do you get a bash shell script to run commands such as passwd where the user is prompted for input?  I want to run the command all in one go, with the relevant inputs provided in the script itself.

Thanks in advance.
0
Comment
Question by:bsufs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:JYoungman
ID: 1638041
You have to use expect(1) to do that.

0
 

Author Comment

by:bsufs
ID: 1638042
Could you tell me a bit more about Expect - what is it, how do I use it, etc.

Thanks very much.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638043
man expect

;-)
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:bsufs
ID: 1638044
Is this the only way to do it, because we don't appear to have expect.

What I am really looking for is a way of changing a password from a script.  The passwd command prompts you for inputs so it is no good calling this from a script.
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1638045
Did you install the package?  It comes with most Linux distributions.

And yes, this is basically the only technique for doing it, unless you are root in which case you can just manipulate /etc/passwd directly.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638046
What's exactly your problem calling passwd from within a script?
0
 
LVL 2

Expert Comment

by:richrussell
ID: 1638047
Expect is in /usr/bin.

Alternatively write a small C program to get your input along the lines of
"scanf "%s",&name; return name" - and use this in your script. Not very portable, but a quick fix anyway...
0
 

Author Comment

by:bsufs
ID: 1638048
I can manipulate /etc/passwd directly but this doesn't help when setting the password because it needs to be encrypted.  The idea was that I would use a script to insert a new entry directly in /etc/passwd and then change the password.

The problem with calling passwd from within a script is that it needs inputs from the user.  I want to call it from within a script and complete the whole process, i.e. change the password, using information from the script and not prompting for inpts.  Basically I just want to run a script telling it to change the password of user xyz to abc and let it run on its own without requiring extra input.
0
 
LVL 3

Expert Comment

by:marcelofr
ID: 1638049
Your problem is that you DO need a tty (or pseudo tty) because passwd command reads directly from it and not from stdin... I can send you a little sample if you want...

0
 
LVL 3

Accepted Solution

by:
marcelofr earned 200 total points
ID: 1638050
Here's my sample:

#!/usr/bin/expect

set oldpass [lindex $argv 0]
set newpass [lindex $argv 1]
spawn /usr/bin/passwd
expect word:
sleep 1
send $oldpass\r
expect word:
sleep 1
send $newpass\r
expect word:
sleep 1
send $newpass\r

You get the idea...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question