如何执行命令ls $ PATH * .log并将结果存储在变量中?

时间:2016-11-14 11:49:41

标签: shell unix scripting

如何存储" ls $ PATH * .log"并将结果存储在一个变量中,如LAST =" ls $ PATH * .log" ??

感谢

#!bin/sh
var="$(ls -l $1*.log)" work correctly
echo "${var}" <-i can view list file

file= /my/path/$1*.log
var="$(ls -l $file*.log)" <- dont work

为什么?

1 个答案:

答案 0 :(得分:1)

您的代码:

file= /my/path/$1*.log #you have already defined pattern here
var="$(ls -l $file*.log)" #this line will try to list /my/path/$1*.log*.log

应该解决这个问题:

file=/my/path/$1*.log #also removed spaces after/before = sign
var="$(ls -l $file)" #this line will tri to list /my/path/$1*.log

OR

file=/my/path/$1 #also removed spaces after/before = sign
var="$(ls -l $file*.log)" #this line will try to list /my/path/$1*.log