Solved

Shell script function

Posted on 2015-02-12
2
86 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

813 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

14 Experts available now in Live!

Get 1:1 Help Now