Shell scripts automate interactive input by sending a \ n string or expect script

Source: Internet
Author: User

Writing a shell script inevitably encounters the steps that require interactive input instructions:

Method One:

# Cat Action.sh
#!/bin/sh
Read-p "Enter Number:" No;
Read-p "Enter Number:" Name;
echo you have entered $no, $name;

# sh action.sh
Enter Number:chavin
Enter number:king
You have entered chavin,king

# echo-e "chavin\nking\n" | SH action.sh
You have entered chavin,king

Or

# echo-e "chavin\nking\n" > Input.data

# Cat Input.data
Chavin
King

# sh action.sh < Input.data
You have entered chavin,king

Case 1: Use interactive automation to change passwords for users:

# Useradd Chavin0101

# echo-e "dbking588\ndbking588\n" | passwd chavin0101
Changing password for user chavin0101.
New Password:retype New Password:passwd:all authentication tokens updated successfully.

Method Two:

Using expect for interactive task automation, the expect tool is not installed by default in the general release and requires manual installation, which is not demonstrated here.

Shell scripts automate interactive input by sending a \ n string or expect script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.