Solved

Shell script function

Posted on 2015-02-12
2
98 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

705 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