如何让我的 bash 脚本打开 cat 进行输入,然后将输入保存到脚本中的变量中

如何让我的 bash 脚本打开 cat 进行输入,然后将输入保存到脚本中的变量中

我正在尝试制作一个简单的电子邮件脚本,以便其他用户可以从终端给我发送电子邮件。

制作该脚本的部分原因是为了获得学习体验,部分原因是其他用户不太熟悉 Linux,所以我希望他们能够轻松地从终端发送电子邮件,而无需学习程序和语法。

我有 ubuntu 16.04.1 服务器

到目前为止,我正在处理以下工作:

clear
echo -e "Please Enter Subject of email:\n "
read subject

echo -e "\n\nFrom: $USER\nTo: Adminstrator\nSubject: $subject\n\nEnter body of email: \n "
read body
echo -e "\nYour message is as follows:\n"
echo -e "\nFrom: $USER\nTo: Administrator\nSubject: $subject\n\n"
echo -e "$body \n\n"

read -p "Send to Administrator (y/n)?" choice
case "$choice" in
  y|Y ) echo "$body" | mail -s "$subject" [email protected];;
  n|N ) echo "Roger Dodger. Email canceled";;
    * ) echo "invalid";;
esac

它可以做的事情:

  • 请求输入主题和正文

  • 将输入发送到各自的变量。

  • 调用这些变量,并发送电子邮件。

我希望它能够做的事情:

  • 允许按 Enter 键转到新行

  • 允许退格

我去过很多地方两次,我想我错过了什么,或者缺乏一些关于如何整合这些功能的基本知识。我尝试使用带分隔符的 read、带分隔符的 cat while 循环(不管是什么),我已经设置了变量、别名。我读过手册页http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html http://ss64.com/bash/read.html 我尝试过通过管道传输它并用 $() 替换,我尝试过将输入发送到一个文件并调用该文件。

我没什么主意了。

当使用 cat 时,我根本无法让终端提供要输入的部分,但我可以让它提供读取提示。

我想我想要类似

cat >> SEND && echo | ${BODY) 

然后在脚本调用它的地方,只需提供 ${BODY} 变量。

在终端上,我成功运行:

cat << SEND && echo | ${BODY} && echo "${BODY}"

这使我能够输入多行,直到我输入 SEND,然后粘贴输出正确的 ${BODY} 变量。

任何想法都将受到赞赏。

答案1

我认为您要求的是不可能的。使用 时read,您只能使用非常有限的文本编辑功能,它根本不是为您想要它做的事情而设计的。即使您可以找到允许退格和换行的方法,这仍然是一种非常麻烦的让用户提供输入的方式。期望用户直接在正在运行的程序中输入文本很少是一个好主意。我建议另一种选择:

  1. 让你的脚本从命令行读取主题。
  2. 让它从输入文件中读取正文。

这样,您的用户就不需要手动输入所有内容,您的脚本可以自动发送多封电子邮件,大大提高了它的实用性,并且您的用户如果输入错误也不需要从头开始。最后,他们可以根据需要格式化电子邮件正文。例如,您可以执行以下操作:

#!/bin/bash
## Parse command line options. 
while getopts ":s:f:" opt; do
  case $opt in
            s)
                subject="$OPTARG"
                ;;
            f)
                file="$OPTARG"
                ## Exit if the file isn't a file or isn't readble               
                if [[ ! -r "$file" && ! -f "$file" ]]; then
                    echo "File doesn't exist or isn't readble"
                        exit
                fi
                ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2;;
  esac
done
## Read the email body
body=$(cat "$file")

## Inform the user. The "\033[1m" starts bold formatting and the
## "\033[0m" ends it. Remove them if you don't want bold. 
echo -e "\033[1mYour message is as follows:\033[0m"
cat<<EoF

From: $USER
To: Administrator
Subject: $subject

$body

------------------
EoF

read -p "Send to Administrator (y/n)?" choice
case "$choice" in
  y|Y ) echo "$body" | mail -s "$subject" [email protected];;
  n|N ) echo "Roger Dodger. Email canceled";;
    * ) echo "invalid";;
esac

然后,您为其提供主题和输入文件。例如,如果您有一个名为的文件,其message内容如下:

$ cat message 
Dear John,

I hope this email finds you well. I am writing to inquire about the
price of the bridge you are selling. I would love to have a bridge
like that in my garden since I think it would make my cows look
larger.

Looking forward to hearing from you,

best,

Jack. 

你可以像这样运行脚本:

$ foo.sh -s "about that bridge..." -f message 
Your message is as follows:

From: terdon
To: Administrator
Subject: about that bridge...

Dear John,

I hope this email finds you well. I am writing to inquire about the
price of the bridge you are selling. I would love to have a bridge
like that in my garden since I think it would make my cows look
larger.

Looking forward to hearing from you,

best,

Jack. 

------------------
Send to Administrator (y/n)?

或者,如果您坚持让用户即时编写消息,则可以使用系统上可用的默认编辑器:

#!/bin/bash

clear
echo -e "Please Enter Subject of email:\n "
read subject

## create a temp file
tmpfile=$(mktemp)

cat<<EoF

Press Enter to open an editor where you can write the body of the email.
When finished, save and exit.
EoF
read

## Open the editor
"$EDITOR" "$tmpfile"

## Read the body and delete the tmp file
body=$(cat "$tmpfile")
rm "$tmpfile"
## Inform the user. The "\033[1m" starts bold formatting and the
## "\033[0m" ends it. Remove them if you don't want bold. 
echo -e "\033[1mYour message is as follows:\033[0m"
cat<<EoF

From: $USER
To: Administrator
Subject: $subject

$body
EoF

read -p "Send to Administrator (y/n)?" choice
case "$choice" in
  y|Y ) echo "$body" | mail -s "$subject" [email protected];;
  n|N ) echo "Roger Dodger. Email canceled";;
    * ) echo "invalid";;
esac

最后,如果您坚持要让您和用户的生活变得比本该的更加困难,您可以使用cat > $tmpfile将正文写入 tmpfile,告诉您的用户在写完后点击Ctrl+ :D

#!/bin/bash

clear
echo -e "Please Enter Subject of email:\n "
read subject

## create a temp file
tmpfile=$(mktemp)
## Enter the body
printf '\nPlease enter the body of the email below. Hit Ctrl+D when done.\n\n'
cat > $tmpfile
body=$(cat "$tmpfile")
rm "$tmpfile"
## Inform the user. The "\033[1m" starts bold formatting and the
## "\033[0m" ends it. Remove them if you don't want bold. 
echo -e "\033[1mYour message is as follows:\033[0m"
cat<<EoF

From: $USER
To: Administrator
Subject: $subject

$body
EoF

read -p "Send to Administrator (y/n)?" choice
case "$choice" in
  y|Y ) echo "$body" | mail -s "$subject" [email protected];;
  n|N ) echo "Roger Dodger. Email canceled";;
    * ) echo "invalid";;
esac

相关内容