Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell scripts

Posted on 1998-09-16
10
Medium Priority
?
402 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 400 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

704 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