Dear Forum members,
I am having trouble getting the complete filename (and directory path) in a variable.
Output directory mentioned in the code have three files:
DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_london.out
DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_paris.out
DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_rome.out
******************************************************
Code is as shown below:
I am tryin to get get the full path of the above files (including directory path in variable file_path).
output_dir=$HOME'/country/city/'
while IFS= read -r city_name
do
## None of the below command worked.
file_path=`${output_dir}/*MIGRATE*${city_name}*.out`
# eval file_path=`${output_dir}/*MIGRATE*${city_name}*.out`
# file_path=${eval echo `ls ${output_dir} | grep "MIGRATE"`}
# eval file_path=`ls ${output_dir} | grep "MIGRATE"`
echo $file_path
done < ${city_list}
## city_list is the list of cities for which I want to display details
Can anybody help with the command?
Thanks.
file_path=${output_dir}
files_list=`ls ${output_dir}/*MIGRATE*${city_name}*.out`
1 Like
Thanks Anbu23 for quick reply. The command sent by you is working.
But what is the difference between below two?
file_path=${output_dir}
files_list=`ls ${output_dir}/*MIGRATE*${city_name}*.out`
and
file_path=${output_dir}
files_list=`echo ${output_dir}/*MIGRATE*${city_name}*.out`
both seems to be working..
A very, very simple example:
Last login: Wed May 15 18:04:12 on ttys000
AMIGA:amiga~> ls Au*
AudioScope.Circuits AudioScope.sh
AudioScope.Config AudioScope_1.sh
AudioScope.Manual AudioScope_Quick_Start.Notes
AMIGA:amiga~>
AMIGA:amiga~> echo Au*
AudioScope.Circuits AudioScope.Config AudioScope.Manual AudioScope.sh AudioScope_1.sh AudioScope_Quick_Start.Notes
AMIGA:amiga~> _
You see the difference with
echo "$file_path"
The quotes prevent the shell to do word-splitting on command arguments.
ls
is an external command. The efficient per-line formatter is printf "%s\n"
that 1. is a shell-builtin and 2. does no extra I/O.
What do you want to do with the $file_path?
It can make sense to not expand it too early. For example
file_path="${output_dir}/*MIGRATE*${city_name}*.out"
for f in $file_path
is smarter than
file_path=`ls ${output_dir}/*MIGRATE*${city_name}*.out`
for f in $file_path