Aparna Rajan
Aparna Rajan

Reputation: 143

ls command in UNIX

I have to ls command to get the details of certain types of files. The file name has a specific format. The first two words followed by the date on which the file was generated

e.g.:

Report_execution_032916.pdf

Report_execution_033016.pdf

Word summary can also come in place of report.

e.g.:

Summary_execution_032916.pdf

Hence in my shell script I put these line of codes

DATE=`date +%m%d%y`
Model=Report
file=`ls ${Model}_execution_*${DATE}_*.pdf`

But the value of Model always gets resolved to 'REPORT' and hence I get:

ls: cannot access REPORT_execution_*032916_*.pdf: No such file or directory

I am stuck at how the resolution of Model is happening here.

I can't reproduce the exact code here. Hence I have changed some variable names. Initially I had used the variable name type instead of Model. But Model is the on which I use in my actual code

Upvotes: 1

Views: 7679

Answers (5)

Aparna Rajan
Aparna Rajan

Reputation: 143

There were some other scripts which were invoked before the control reaches the line of codes which I mentioned in the question. In one such script there is a code typeset -u Model This sets the value of the variable model always to uppercase which was the reason this error was thrown ls: cannot access REPORT_execution_032916_.pdf: No such file or directory

I am sorry that i couldn't provide a minimal,complete and verifiable code

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755104

You've changed your script to use Model=Report and ${Model} and you've said you have typeset -u Model in your script. The -u option to the typeset command (instead of declare — they're synonyms) means "convert the strings assigned to all upper-case".

-u When the variable is assigned a value, all lower-case characters are converted to upper-case. The lower-case attribute is disabled.

That explains the upper-case REPORT in the variable expansion. You can demonstrate by writing:

Model=Report
echo "Model=[${Model}]"

It would echo Model=[REPORT] because of the typeset -u Model.

  • Don't use the -u option if you don't want it.

You should probably fix your glob expression too:

file=$(ls ${Model}_execution_*${DATE}*.pdf)

Using $(…) instead of backticks is generally a good idea.

And, as a general point, learn how to Debug a Bash Script and always provide an MCVE (How to create a Minimal, Complete, and Verifiable Example?) so that we can see what your problem is more easily.

Upvotes: 1

Unit1
Unit1

Reputation: 279

Part of question: 
But the value of Model always gets resolved to 'REPORT' and hence I get:

This is due to the fact that in your script you have exported Model=Report

Part of question:
ls: cannot access REPORT_execution_*032916_*.pdf: No such file or directory

No such file our directory issue is due to the additional "_" and additional "*"s that you have put in your 3rd line. Remove it and the error will be gone. Though, Model will still resolve to Report

Original 3rd line :

 file=`ls ${Model}_execution_*${DATE}_*.pdf` 

Change it to

 file=`ls ${Model}_execution_${DATE}.pdf`

Above change will resolve the could not found issue.

Part of question
I am stuck at how the resolution of Model is happening here.

I am not sure what you are trying to achieve, but if you are trying to populate the file parameter with file name with anything_exection_someDate.pdf, then you can write your script as

DATE=`date +%m%d%y`
file=`ls *_execution_${DATE}.pdf`

If you echo the value of file you will get 
Report_execution_032916.pdf Summary_execution_032916.pdf
as the answer

Upvotes: 0

serv-inc
serv-inc

Reputation: 38307

As a workaround

ls | grep `date +%m%d%y` | grep "_execution_" | grep -E 'Report|Summary'

filters the ls output afterwards.

touch 'Summary_execution_032916.pdf'
DATE=`date +%m%d%y`
Model=Summary
file=`ls ${Model}_execution_*${DATE}*.pdf`

worked just fine on

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Upvotes: 0

Enrico
Enrico

Reputation: 835

Some things to look at:

  • type is usually a reserved word, though it won't break your script, I suggest you to change that variable name to something else.
  • You are missing an $ before {DATE}, and you have an extra _ after it. If the date is the last part of the name, then there's no point in having an * at the end either. The file definition should be:

file=`ls ${type}_execution_*${DATE}.pdf`

Try debugging your code by parts: instead of doing an ls, do an echo of each variable, see what comes out, and trace the problem back to its origin.

As @DevSolar pointed out you may have problems parsing the output of ls.

Upvotes: 0

Related Questions