Solved

Shell scripts

Posted on 1998-09-16
10
397 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to mount an LVM partition that is part of a disk image. 3 71
Python variable _ manually assigned 9 80
Hacked File Timestamps 4 61
awk file 6 49
This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

786 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