[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Shell script function

Posted on 2015-02-12
2
Medium Priority
?
123 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Utilizing an array to gracefully append to a list of EmailAddresses
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

650 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