[英]storing the output of ls command in a shell variable
I am using below command to find a most recent file with name "candump"我正在使用以下命令查找名为“candump”的最新文件
ls *.log | grep "candump" | tail -n 1
The output is "candump-2018-04-19_131908.log" output 是“candump-2018-04-19_131908.log”
I want to store the output filename to a variable in my shell script.我想将 output 文件名存储到我的 shell 脚本中的变量中。 I am using the below commands:
我正在使用以下命令:
logfile = `ls *.log | grep "candump" | tail -n 1`
and和
logfile = $(ls *.log | grep "candump" | tail -n 1)
However, both times I am getting the same error, "logfile: command not found".但是,两次我都收到相同的错误,“日志文件:找不到命令”。 Am I doing something wrong?
难道我做错了什么? Any help is appreciated.
任何帮助表示赞赏。
You have to stick the variable name and its value (no space before and after the =
).您必须粘贴变量名称及其值(
=
前后没有空格)。
Try:尝试:
logfile=$(ls *.log | grep "candump" | tail -n 1)
This is working for me.这对我有用。
#!/bin/bash
my_command='ls | grep server.js | wc -l';
my_data=$(eval "$my_command");
echo "value in echo is:" $my_data;
if [ $my_data == "1" ]; then
echo "value is equal to 1";
else
echo "value is not equal to 1";
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.