Solved

Shell script function

Posted on 2015-02-12
2
78 Views
Last Modified: 2015-02-16
Hi,

I use the below function to get the product name in the log.

prod_log {
DATE=$(date +%Y-%m-%d);
product=$(head -1 /home/magento/import*.csv 2>/dev/null) || product=Dummy;
echo $DATE $product
}

Open in new window


This works fine when i receive one import.csv file , and if no import.csv file found i add Dummy to the variable product.

Now i  receive more than 1 import file , eg. import1.csv , import2.csv . At this situation how can i change my function so it reads the latest import file and get the product name.

Thanks
0
Comment
Question by:magento
2 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 40607174
Use

file=$(ls -t import*.csv 2>/dev/null| head -1)

if [[ -z $file ]]
then
   product=Dummy
else
   product=$(head -1 $file)
fi

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40607397
product=$(echo Dummy | cat  $(ls -t import*.csv) | head -1)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now