如何从标准输入到变量以及如何在外壳打印一出(SH,bash)的读取mutliline输入? [英] How to read mutliline input from stdin into variable and how to print one out in shell(sh,bash)?

查看:202
本文介绍了如何从标准输入到变量以及如何在外壳打印一出(SH,bash)的读取mutliline输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是以下内容:


  1. 标准输入在多个线路输入读入变量 A

  2. 请在 A 的各种操作

  3. 管道 A 不失分隔符号( \\ n \\ r \\ t 等)到另一个命令

目前的问题是,我不能读它读命令,因为它停止在新行阅读。

我可以用,这样的标准输入读取

  my_var =`猫的/ dev / stdin`

,但我不知道如何打印它。这样换行,制表符和其他分隔符仍然存在。

我的示例脚本是这样的:

 #!的/ usr / local / bin目录/ bash的A =`猫的/ dev / stdin`如果[$ {#A} -eq 0];然后
        退出0
其他
        猫$ {A} |在/ usr / local / sbin中/ nextcommand
科幻


解决方案

这是为我工作:

  MYVAR =`cat`回声$ MYVAR

大约$ myvar的引号是很重要的。

What I want to do is the following:

  1. read in multiple line input from stdin into variable A
  2. make various operations on A
  3. pipe A without losing delimiter symbols (\n,\r,\t,etc) to another command

The current problem is that, I can't read it in with read command, because it stops reading at newline.

I can read stdin with cat, like this:

my_var=`cat /dev/stdin`

, but then I don't know how to print it. So that the newline, tab, and other delimiters are still there.

My sample script looks like this:

#!/usr/local/bin/bash

A=`cat /dev/stdin`

if [ ${#A} -eq 0 ]; then
        exit 0
else
        cat ${A} | /usr/local/sbin/nextcommand
fi

解决方案

This is working for me:

myvar=`cat`

echo "$myvar"

The quotes around $myvar are important.

这篇关于如何从标准输入到变量以及如何在外壳打印一出(SH,bash)的读取mutliline输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆