LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-01-2007, 10:58 PM   #1
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Rep: Reputation: 38
Script to count lines from 'jobs' not working ...


I wrote the following script ...
Code:
#!/bin/bash

if [ `jobs|wc -l` = 0 ]
        then
                echo 'There are no jobs'
        else
                echo 'There are some jobs'
fi
When I execute it, it always tells me 'There are no jobs', even if there are stopped jobs in the shell. Running "jobs|wc -l" directly at the CLI works as expected.

What's broken in my script?
 
Old 05-02-2007, 12:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

When you start a script, a new shell is started. That new shell has no jobs (waiting, running or stopped).

You could remove the #!/bin/bash line and parse the file instead of executing it (. filename instead of filename or ./filename).

Hope this helps.
 
Old 05-02-2007, 01:38 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
You can also use shell functions or aliases, like that one:

Code:
$ alias j='echo there are $([[ $(jobs|wc -l) -eq 0 ]] && echo no || echo some) jobs'
$ j
there are no jobs
$ sleep 10 &
$ j
there are some jobs
$
 
Old 05-02-2007, 08:53 AM   #4
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Original Poster
Rep: Reputation: 38
Thank you, both of you, for the options you've given. I'm looking into both.

However, can the parsing explanation be explained in more detail?

It seems to me that the current major hurdle is retrieving the value I desire from within the current shell context, not a new one.

If I remove the #!/bin/bash from my code, will that allow it to run in the current shell? I tried it with it and without, with same results.

Last edited by SlowCoder; 05-02-2007 at 09:24 AM.
 
Old 05-02-2007, 10:18 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

If you edit your file (remove the hash-bang) and make it like this:
Code:
if [ `jobs|wc -l` = 0 ]
        then
                echo 'There are no jobs'
        else
                echo 'There are some jobs'
fi
And parse it:
Code:
 $ ls -l example 
-rw-r-----  1 druuna internet 137 May  2 16:07 example

$ . example 
There are no jobs
As you can see the file doesn't even need execute permissions. Do mind the space between the dot and the filename.

The major difference between parsing and executing a file is the way the shell is used. Parsing is done in the shell you are in, while executing will start a new shell and runs the program in the new shell.
 
Old 05-02-2007, 10:56 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Quote:
Originally Posted by SlowCoder
If I remove the #!/bin/bash from my code, will that allow it to run in the current shell?
No, the prefix is unrelated, it's the way you launch the script that matters.
Quote:
I tried it with it and without, with same results.
Indeed, the #! prefix is only used when the script is executed by the O/S as an executable program to tell what command interpreter to use.
When you run it with the shell "." command, the whole first line is ignored as it starts with "#" which is the comment prefix.
 
  


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
count lines beginning with 50 chloraldo Programming 3 08-18-2006 09:30 AM
Recursively count lines of code in source dir heirarchy michaelsanford Programming 8 04-01-2006 05:59 PM
count total number of lines in several files xushi Programming 5 11-12-2005 05:42 PM
command to count how many lines in a file? dr_zayus69 Linux - Software 3 01-06-2005 11:42 AM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 09:26 PM


All times are GMT -5. The time now is 01:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration