LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 05-01-2013, 04:44 PM   #1
jkevinm80
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Script Challenge


Ok all you script genies and geniuses out there. I am just starting a new project for a new client and they hit me with this today. I KNOW I have seen this behavior before on both Unix and Linux systems, but I can't remember why it behaved as I am about to describe.

This is part of a script that clears old ftp files out of an EDI transfer directory so that the script that creates the file will not believe there is a conflict the next time it runs. It hangs, every time it runs, at the "else". Here is the first part of the script;

psHistory=acc0/pshistory.log
killHistory=/acc0/killHistory.log
emailMessage=/acc0/emailMessage.txt
secondsToWait=600
emailDistro='name@domain.com,name1@domain.com',

##
###If file doesn't exist then create it.
##
if [ -f "${psHistory}" ]
then
ps -ef | grep ^lds |grep LVEX | cut -c15-20 | while read pidSpec
do
echo "$ (date + "$s") ${pidSpec}" $(psHistory)
else
fi
#

This script hangs every time it gets to else. Any ideas campers???? All donations will be accepted and appreciated!

I remember having this exact same issue about 10 years ago on a Unix server that was running the same application set, but I can't for the life of me remember how we resolved it.
Thanks!
 
Old 05-01-2013, 05:26 PM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,618
Blog Entries: 1

Rep: Reputation: Disabled
Code:
=/acc0/
is true everywhere EXCEPT for psHistory, is that by design?
 
Old 05-01-2013, 07:51 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Code:
if [ -f "${psHistory}" ]
then
    ps -ef | grep ^lds |grep LVEX | cut -c15-20 | while read pidSpec
    do
        echo "$ (date + "$s") ${pidSpec}" $(psHistory)
else
fi
using code tags is easier to read https://www.linuxquestions.org/quest...do=bbcode#code.

As per Habitual for leading '/' missing.
Also, where's the 'done' for the while loop?
I also recommend [[ ]] http://tldp.org/LDP/abs/html/testcon...l#DBLBRACKETS; more robust.

You could add 'set -xv' at the top of the script to get the parser to show what its seeing/doing.
 
Old 05-05-2013, 09:18 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
There's no command in the else part for the script to execute. That will probably result in an error. Indeed, bash doesn't allow you to have such empty fields at all, erroring out as soon as the script is read, so you must be using some other shell. In any case, if there's no command to run, just eliminate the else keyword entirely.

If you aren't using a standard Linux and bash, be sure to specify the environment you're using. It affects the advice we can give you.

It also helps to supply examples of what the inputs and desired outputs look like. I have no idea what the format would be for the desired line from ps, or what characters 15-20 are. You could probably configure ps to print out exactly what you want directly, with the proper options.


Now for some more comments on the scriptlet itself:

psHistory is not a command, but a file, correct? So don't put it in $() command substitution brackets. If those are meant to be ${..} variable brackets, you'd be better off just removing them entirely. The full bracketed form of a variable shouldn't be used except with substitutions or in relatively rare concatenating circumstances. It otherwise does nothing but clutter up the code and make this kind of error more likely.

Your filter for ps uses three separate processes when a single awk expression can do the whole thing more efficiently.

Your date command is wrong. There is no "$s" variable. I think you want date +%s instead, and properly-formatted substitution brackets.

Code:
psHistory=/acc0/pshistory.log
killHistory=/acc0/killHistory.log
emailMessage=/acc0/emailMessage.txt
secondsToWait=600
emailDistro='name@domain.com,name1@domain.com',

##
###If file doesn't exist then create it.
##

if [ -f "$psHistory" ]; then

    ps -ef | awk '/^lds/ && /LVEX/ { print substr($0,15,6) }' |\
    while read pidSpec; do
        echo "$(date '+%s') $pidSpec" "$psHistory"
    done

fi
If it's possible to code for /bin/bash (or ksh) specifically, instead of the posix-only /bin/sh, then we could certainly make it even cleaner, with process substitution, double-bracket tests, and more advanced parameter substitutions available to help us out.
 
1 members found this post helpful.
Old 05-05-2013, 09:34 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Re null cmd; actually you can use ':'
Code:
v=0
if [[ $v -eq 1 ]]
then
    echo 1
else
    :
fi
which will 'work' and not throw an error.
However, I normally re-write the logic to eliminate the need in the first place.

EDIT: actually, when I said null, I really meant that ':' just returns a 'true' value. HTH

Last edited by chrism01; 05-05-2013 at 09:02 PM. Reason: Add 'true' clarification
 
Old 05-05-2013, 11:32 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,675
Blog Entries: 54

Rep: Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954Reputation: 2954
...additionally (unless you need compatibility with platforms devoid of it)
Code:
   ps -ef | grep ^lds |grep LVEX | cut -c15-20
means you didn't come across 'pgrep'.
 
Old 05-13-2013, 09:18 PM   #7
jkevinm80
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Ok Campers! I have to thank each of you because ALL of these suggestions ended up being part of the solution. The first part was making sure the firewall was open. When the client moved to Active FTP, nobody bothered to notify those pesky guys in security. That stopped about 60%. Minor adjustments made to the scripts from each of the suggested solutions ended up accounting for another 35%. What we have left with is apparently related to quirks in the code of the original application. As this is being replaced with something better by the end of the year, we can live with the 5%.

Thanks for all your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Challenge in Bash script with read variable manya Programming 2 07-27-2009 12:00 AM
Shell Script / Awk help for a challenge cmontr Programming 30 06-03-2008 07:11 PM
Mail challenge in script anil3 Linux - General 3 09-25-2006 10:13 AM
how do i script this (a challenge indeed) mrgreaper Linux - General 22 09-17-2006 01:35 PM
a challenge for you derfaust Programming 3 03-12-2004 01:23 AM


All times are GMT -5. The time now is 01:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration