如何在shell中存储grep命令的结果 [英] how to store grep command result in some variable in shell

查看:1706
本文介绍了如何在shell中存储grep命令的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出主目录中的目录和文件的数量,并且想要在shell脚本中存储一些变量。

  command =ls -l | grep -c \rahul。* patle \\ \\
eval $命令

我想将此计数存储到某个varibale计数中。我怎么做到这一点。

解决方案

将命令输出存储到变量中的语法是 var = $($ command)



所以你可以直接做:
$ b $ pre > result = $(ls -l | grep -crahul。* patle)

变量 $ result 将包含匹配的数量。


I want to find out number of directories and files in home directory and want to store count some variable in a shell script. I am using following set of commands.

command="ls -l | grep -c \"rahul.*patle\""
eval $command

i want to store this count into some varibale count. How can i do this.

解决方案

The syntax to store the command output into a variable is var=$(command).

So you can directly do:

result=$(ls -l | grep -c "rahul.*patle")

And the variable $result will contain the number of matches.

这篇关于如何在shell中存储grep命令的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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