LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03: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, 04:26 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
=/acc0/
is true everywhere EXCEPT for psHistory, is that by design?
 
Old 05-01-2013, 06:51 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08:18 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 08:34 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 08:02 PM. Reason: Add 'true' clarification
 
Old 05-05-2013, 10:32 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...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, 08: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



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-26-2009 11:00 PM
Shell Script / Awk help for a challenge cmontr Programming 30 06-03-2008 06:11 PM
Mail challenge in script anil3 Linux - General 3 09-25-2006 09:13 AM
how do i script this (a challenge indeed) mrgreaper Linux - General 22 09-17-2006 12:35 PM
a challenge for you derfaust Programming 3 03-12-2004 12:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:56 AM.

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
Open Source Consulting | Domain Registration