简体   繁体   English

Linux bash Shell脚本:使用字符串创建命令附加该怎么做?

[英]Linux bash Shell scripting : Creating command using string Append how to do that?

Hi I want to achieve #cat sample.txt > abc.txt 嗨,我想实现#cat sample.txt> abc.txt

But "> abc.txt" am getting as argument. 但是“> abc.txt”越来越多了。

How to cascade these two strings and execute the combined one. 如何级联这两个字符串并执行合并的字符串。

I tried the below steps 我尝试了以下步骤

/home/root# export str=" > abc.txt;"    

/home/root# echo $str 

> abc.txt;

/home/root# echo "cat sample.txt $str" 

cat sample.txt  > abc.txt;

/home/root# `echo "cat sample.txt $str"`

Hello world

cat: can't open '>': No such file or directory
cat: can't open 'abc.txt;': No such file or directory

like this? 像这样?

#!/bin/bash
args="> foo"
command="date"
eval "$command $args"
cat foo

use of eval is not really a recommended method but sometimes it comes in handy for doing things in a quick and dirty way. 实际上,不建议使用eval方法,但有时可以用它方便快捷地处理问题。 I will probably get downrated for suggesting this. 我可能会因为建议这个而被贬低。 Be aware of its side effects. 注意它的副作用。

如果您需要从命令行执行此操作,请输入:

cat sample.txt | tee $str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM