Execute a command in different directory through Unix Script


 
Thread Tools Search this Thread
# 1  
Execute a command in different directory through Unix Script

Hi Gurus,

I have the below requirement,

Execute an unix script which will pick the latest file from the archive directory and do a grep (on multiple patterns) on that file.

processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE"
filename = 'ls Check* | sort -n -k 2 | tail -1'

The processing directory contains the list of archive files and the file name command will retrieve the latest timestamp file and the below command does the egrep.

egrep 'CHECKS NOT BALANCED|
PAYMENTS ARE OUT OF BALANCE' filename

Problem:
1. I am not sure how to execute the fileName command in the processing directory and look for the egrep on the retrieved file.
2. When executed the egrep on the file, the command expects the file to be set to have the execute privilges, how can i set that during run time.

Your thoughts and inputs please.
# 2  
Code:
 
#!/bin/sh
 
processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE"
filename=`ls -1tr $processingDir/Check* | tail -1`
 
egrep 'CHECKS NOT BALANCED|PAYMENTS ARE OUT OF BALANCE' $filename


Last edited by rdrtx1; 10-18-2012 at 01:25 PM..
# 3  
bash

Hi,

Try this one,

Code:
#! /usr/bin/env bash

processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE"
filename="ls $processingDir/Check* | sort -n -k 2 | tail -1"

file=$(eval $filename)
echo "File Name:${file}"

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 4  
how your filename looks like ?

post the sameple filenames. and is that having any timestamp ?

you can retrieve the last filename like

Code:
filename=$(ls -lrt Check* | awk '{a=$NF}END{print a}' )

if you want to check whether the file has read permission ( egrep needs ), you can check like...

Code:
[ -r ${filename} ] || chmod 444 ${filename}
egrep  'CHECKS NOT BALANCED|PAYMENTS ARE OUT OF BALANCE' ${filename}

make sure, the user has rights to change the file permission

---------- Post updated at 09:38 PM ---------- Previous update was at 09:38 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
 
#!/bin/sh
 
processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE"
filename=`ls -1tr $processingDir | tail -1`
 
egrep 'CHECKS NOT BALANCED|PAYMENTS ARE OUT OF BALANCE' $filename

ls -lrt | tail -1 ---> will not give the filename
This User Gave Thanks to itkamaraj For This Post:
# 5  
Thank you itkamaraj.

Check_Report.txt.10182012.041335
Check_Report.txt.10172012.041315
Check_Report.txt.10162012.041325

Can you please let me know how could i execute the command in that folder and do an egrep.
# 6  
Code:
#!/bin/sh
 
processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE"
filename=$(ls -lrt $processingDir | awk '{a=$NF}END{print a}')

echo "Filename : ${filename}"
echo "Checking if ${filename} has read permission for the user. If not assign read permission"

[ -r ${filename} ] && echo "File ${filename} has read permission" || chmod 444 ${filename}

#Now do egrep
egrep 'CHECKS NOT BALANCED|PAYMENTS ARE OUT OF BALANCE' ${filename}

if you want to grep the today's file (only one file per day), then

Code:
filename=$(ls Check_Report.txt.$(date +%m%d%Y)*)

This User Gave Thanks to itkamaraj For This Post:
# 7  
Hi Kamaraj,

Again for egrep we need to provide the complete path with the file name right???

I guess providing only the file name will result in file not found error.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

2. Shell Programming and Scripting

How to execute previous command in UNIX?

Hi, I was able to use !! on the console. But when I used !! in the run.sh, it says something like command not found. (3 Replies)
Discussion started by: alvinoo
3 Replies

3. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

4. Shell Programming and Scripting

How to execute korn shell script from different directory

Guy’s , I need to run korn shell script from different directory, usually I run the script using ./details.ksh in the same directory but now I need to run the file and process details using awk code. Now I am running the script this way but with no luck Directory = home/users/work ... (3 Replies)
Discussion started by: James_Owen
3 Replies

5. Shell Programming and Scripting

Cannot execute Unix command in a simple perl script

Am trying to lean perl scripting in Unix OS to automate my tasks. Please find the below perl script i have tried #!/usr/bin/perl -w print "Please Enter the VG name to be checked:"; $A = <>; print "Please Enter the free size to be checked in GB:"; $B = <>; $vgcheck = `vgdisplay... (7 Replies)
Discussion started by: jayachandran87
7 Replies

6. Shell Programming and Scripting

How to execute PMCMD command in Unix?

Hi All, I am trying to run the PMCMD commend to execute the informatica workflow but while executing from my home directory i am getting below error ksh: pmcmd: not found I executed from my server/bin directory too. I am getting the same error. My commend is: pmcmd startworkflow -sv... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

Execute a unix command

Hi, Please suggest me to execute unix command. Input : 2011-01-01 00:00:00 Required output : 20110101000000 YYYYMMDDHH24MISS Regards Akshu (4 Replies)
Discussion started by: akshu.agni
4 Replies

8. Shell Programming and Scripting

How to execute script on files in another directory?

Hi Guys, Is there any way I can execute my bash script on files in a different folder than what the script is in? Here is an excerpt of my script: #!/usr/bin/bash input_path="/cygdrive/c/files" output_path="/cygdrive/c/files/data" #script uses files from /cygdrive/c/files directory,... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. UNIX for Dummies Questions & Answers

How to execute a command on UNIX?

Hi guys, I'm completely new to UNIX and Linux and all that stuff. I've been using webhosting (shared) for some time years now and recently I've wanted to carry out some tasks that have had some commands which are not familiar to me. One was to install Mailman and the other to copy files from one... (6 Replies)
Discussion started by: BenA
6 Replies

10. Shell Programming and Scripting

execute command unix with java:possible or not?

hi everybody, i have a small unix script and i want to use it with java, so how can i executte unix command with java (if it is possible): in fact i want to use some values returned by this script with java swings. is it possible (of course everything is possible) but how can i do? thank you in... (4 Replies)
Discussion started by: mips
4 Replies

Featured Tech Videos