Catanzaro Catanzaro - 25 days ago 7
Bash Question

how to execeute a command ls $PATH*.log and store result ina variable?

hi how to store result of a "ls $PATH*.log" and store result in a variable like LAST="ls $PATH*.log" ??

thanks

#!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


why?

Answer

your code:

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

should be fix to this:

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