LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 09:58 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
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-01-2007, 11:24 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 12:38 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 07:53 AM   #4
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
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 08:24 AM.
 
Old 05-02-2007, 09:18 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09:56 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:22 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