Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 26 Oct 2014, 04:42
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Ok in rxvt but not in script
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Author Message
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Fri 01 Jan 2010, 12:34    Post_subject:  Ok in rxvt but not in script
Sub_title: Why the inconsistancy?
 

This works perfectly in rxvt but fails in a script.....it seems to get mixed up over spaces.

Code:
ps | grep 'wget -c' | grep -v grep | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status


line from ps...
9613 root 1404 S wget -c -o /tmp/tart.test -O /mnt/hda2/test.iso http://distro.ibiblio.org/pub/linux/distributions/puppy

expected result
test.iso
gives full line...breaking down the command truncation happens in the wrong places.

yet...
Code:
   ps | grep 'cclive -c' | grep -v grep | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status


working on
9684 root 1876 S cclive -c -f mp4 -O /mnt/hda2/test.mp4 http://www.youtube.com/watch?v=Sv5iEK-IEzw

gives expected
test.mp4

confused
mike

ok ..the eventual workaround was to turn around the wget commant to give
11061 root 1400 S wget -c -O /mnt/hda2/test.iso -o /tmp/tart.fhsgjsgfjs http://distro.ibiblio.org/pub/linux/distributions/pup
but I would like to know why there was a difference between the command line and script.
Back to top
View user's profile Send_private_message 
amigo

Joined: 02 Apr 2007
Posts: 2262

PostPosted: Fri 01 Jan 2010, 13:18    Post_subject:  

The output from ps, as with many other commands is formatted with TABs, so you need to grep for the line you need, then echo that and pipe through sed. The echo'ing converts tabs to spaces, eliminates duplicates and eliminates any leading or trailing spaces:
STATUS=$(ps | grep 'wget -c' | grep -v grep)
echo $STATUS | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Fri 01 Jan 2010, 17:17    Post_subject:  

Ah ok..thanks for the quick answer.
I tend to build up the command line to get the desired result then copy over to geany ...so basically I need to split apart to get consistant results .
I guess I've been running on luck rather than design Very Happy

Ok I will implement (I don't like workarounds) and go forth Smile

regards

mike
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Fri 01 Jan 2010, 17:30    Post_subject:  

Nope same behaviour...I will try on something else ...seems like a bash problem Sad

mike
Back to top
View user's profile Send_private_message 
Patriot


Joined: 15 Jan 2009
Posts: 734

PostPosted: Mon 04 Jan 2010, 16:01    Post_subject:  

Hmmm .....

mikeb,

Could it be that the ps shell script is causing this for you? ... I've always try to go direct to ps-FULL or busybox ps or when I'm sure of it, specify options, ie: ps ax ...


Rgds
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Mon 04 Jan 2010, 16:55    Post_subject:  

A possibility indeed...well I've got a meeting coming up Wednesday so I have to prepare including persuading some accounting software to install to a local web server...oh joy...so I will pick this one up in a while.
When I broke it down I would get results like
test.i or test.iso h at the end.
Something unpleasent from ps could be a possibility.

regards and a fun New Year

mike
Back to top
View user's profile Send_private_message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 853
Location: SE

PostPosted: Mon 04 Jan 2010, 17:06    Post_subject:  

Your sed-search-word "http" in most cases (with the reasonably short log- and output- file names you gave above) ends up around the truncation point of grep's. Try
Code:
ps w
so as not to exclude your search-word. Doesn't explain your experienced difference between cli and script, though, unless there was an extra space or something ...

EDIT: Seems like (at least busybox 1.6.1) ps, without the wide-switch, only pipes 80 char's ...

fwiw Smile/
MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP

Edited_time_total
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Mon 04 Jan 2010, 18:07    Post_subject:  

Thanks for the input....I seem to be running on the busybox ps without any options....I will check behaviour with the full version.
Indeed I might take a different approach to tracking created processes anyway...
regards
mike
Back to top
View user's profile Send_private_message 
Shel

Joined: 11 Apr 2009
Posts: 103
Location: Seattle, WA, USA, or Southern France

PostPosted: Tue 05 Jan 2010, 07:02    Post_subject:  

IIRC, the IRIX version of Unix includes a program called pgrep which reports whether a program is currently running. I stole that idea and implemented it as a shell script in Solaris, and it proved very handy. I'd give you the source, but all my Solaris machines are gone, and the tape archives of their contents are 8,000 miles away.

In general, though, it parsed the output of ps, as you're doing, and terminated with the appropriate exit code (and returned the job number[s] on stdout) if it found the program requested. As a stand-alone program, it could then be called by other programs to be sure I didn't run more than one copy of something, etc.

-Shel
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Tue 05 Jan 2010, 10:38    Post_subject:  

Quote:
In general, though, it parsed the output of ps, as you're doing, and terminated with the appropriate exit code (and returned the job number[s] on stdout) if it found the program requested. As a stand-alone program, it could then be called by other programs to be sure I didn't run more than one copy of something, etc.

ooo that sounds better and it does seem it has been ported to linux....I do find this all a bit clumsy.....tracking processes when gtkdialog is used is a challenge and using text files is just plain messy...why have variables if everything has to be stored on the hard drive....ooo ranting again Very Happy

mike
Back to top
View user's profile Send_private_message 
Shel

Joined: 11 Apr 2009
Posts: 103
Location: Seattle, WA, USA, or Southern France

PostPosted: Wed 06 Jan 2010, 01:48    Post_subject:  

mikeb wrote:
I do find this all a bit clumsy.....

The classical Unix programming philosophy is a bit different from what you might find in other systems; the emphasis is on small programs that do one thing well, and pass their output along is a way that makes them easy to use in various combinations. Typically, you then use all these little programs as "commands" in larger programs, which become "commands" in still larger programs, etc.

Once you have your "pgrep" program built, either as your own script or something ported from another system, you have it, and it becomes another building block. It's Lego for grownups.

-Shel
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Wed 06 Jan 2010, 08:40    Post_subject:  

Quote:
It's Lego for grownups.

now that sounds more appealing.
I quite like using bash it was just this particular function that was a bit thorny.

Thanks for the input

mike
Back to top
View user's profile Send_private_message 
Shel

Joined: 11 Apr 2009
Posts: 103
Location: Seattle, WA, USA, or Southern France

PostPosted: Wed 06 Jan 2010, 09:05    Post_subject:  

I decided to implement my own pgrep, and found that various ps commands act differently from the command line than from a script. It's a real WTF, but I think that's what started this whole thread!

In any case, here's what I'm using ...
Code:

ps -e | grep " ${1}$" | grep -v ' grep ' | grep -v '<defunct>' | awk '{print $1}'

... all one one line.

I'm sure it could be done more elegantly, but this works, is reasonably fast, and finds the exact match.

The first grep finds the word given as the argument to the script, with a space in front and a newline after; the other two greps just get rid of stuff you don't want, while the awk prints the job number to stdout.

-Shel
Back to top
View user's profile Send_private_message 
mikeb


Joined: 23 Nov 2006
Posts: 8368

PostPosted: Wed 06 Jan 2010, 13:50    Post_subject:  

Quote:
and found that various ps commands act differently from the command line than from a script.

so I was not going barmy then...oh too late Very Happy
Well I'm free to play again with this Smile cheers

mike
Back to top
View user's profile Send_private_message 
amigo

Joined: 02 Apr 2007
Posts: 2262

PostPosted: Wed 06 Jan 2010, 14:49    Post_subject:  

ps -e |grep "${1}$" | egrep -v '( grep |<defunct>)' | awk '{print $1}'
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2377s ][ Queries: 11 (0.1628s) ][ GZIP on ]