Solved

Shell script function

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
awk sed 8 64
Migrating php-mysql-jquery application to Microsoft Azure Cloud 7 38
Trying to install php56 on CentOS 7 get GPG error 5 27
LINUX Field Separators 7 34
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

839 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