Solved

Shell scripts

Posted on 1998-09-16
10
399 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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