A Perl Syntax Question.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A Perl Syntax Question.
# 1  
Old 02-11-2014
A Perl Syntax Question.

Greetings!

Here's what I believe is a "simple one" for the community tonight Smilie

What I'm trying to do is assign a "true/false" value to a variable depending upon whether a named process (some-process) exists; and then test for this value in the succeeding logic. I banged my head against the wall forever this afternoon trying to get it right:
Code:
$value = '$(pgrep some-process | wc) -gt 0';

if ($value)
{
  do something;
}

else
{
  do something else;
}
.
.
.

Can't quite get the syntax here. Anyone have a quick moment?

Thanks --

EDIT:
Looking @ pgrep a little closer, would something like this work just as well (or better):
Code:
'$(pgrep some-process > /dev/null)';

Smilie

Last edited by LinQ; 02-11-2014 at 10:30 PM.. Reason: Added material . . .
# 2  
Old 02-11-2014
1. Use backticks (or the system keyword) to execute shell commands
2. grep has a -c switch that will give a count of matching lines for each input file.

Code:
$value = `pgrep -c some_process`;
($value > 0) ? ($value = 1) : ($value = 0);

if ($value) {
  do_something;
}
else {
    do_something_else;
}

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-12-2014
Thanks for the tips, balajesuri. All tucked away for safe keeping Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question regarding quotation syntax

Hey guys, my first post on UNIX Forums(much overdue IMO)! I've got this bit of code that doesn't seem to be working correctly for an Android app I'm working on: "screen -S gmod1 -p 0 -X stuff " & "" & command.text & "`echo -ne '\015'`""" Basically it types command.text(variable determined... (4 Replies)
Discussion started by: stingwraith
4 Replies

2. Shell Programming and Scripting

Perl String Replacement Syntax Question . . .

Greetings! I've been tooling about with Perl to make a few string replacements in some files; and seem to have run into a bit of a squeeze :) Beginning with a simple text file, test.txt, we have the following content to be worked:Now, not wanting to have anyone feel left out, I decided that... (6 Replies)
Discussion started by: LinQ
6 Replies

3. UNIX for Dummies Questions & Answers

Perl syntax

Query with perl syntax Aim: is to change a perl script to use a new file I was required to replace - entries \"$entries\" with -lib <full_path_to_filename> So in the code detector.pm sub rundetector { my $class = shift; mkdir($resultDirectory); my... (3 Replies)
Discussion started by: sa@@
3 Replies

4. Shell Programming and Scripting

Perl syntax

I'm a newbie to perl scripting. Can someone please explain the syntax below. Specifically what does -> and => do? $tz->site( => $site); (10 Replies)
Discussion started by: scj2012
10 Replies

5. Programming

Perl syntax question

Hallo everybody, I have a following problem - I'm doing a map funciont to fill in a HTML table and I want to use some radiobutton groups. Unfortunatelly, they are grouped by names, so I have to add some "counter" that will divide one row from another, and I'm using CGI.pm for generating the... (3 Replies)
Discussion started by: duskos
3 Replies

6. Shell Programming and Scripting

Question about syntax error

first of all.. sorry about all the question bombing.. im bored atm so im currently playing around with sh scripting hehe s = `expr ls -s Documents | grep Music | awk '{ print $1 }' ` t = `expr $t + $s` it give syntax error s not found t not found lol... any idea why? (7 Replies)
Discussion started by: Nick1097
7 Replies

7. Shell Programming and Scripting

tar -C syntax question

I am writing a perl script to tar multiple files (in unix) from a given directory to a given output directory. I do NOT want the file path included in the tar, so I've flagged the -C option. Example: tar -cvf tar/1.tar -C htmp/source/ 1-1-1.xml However, I need to do this for a number of target... (3 Replies)
Discussion started by: michanjohns
3 Replies

8. Shell Programming and Scripting

perl syntax help

Im new at scripting and im trying to write a script using perl that will make sure there are 2 command line integer arguments and then check if the 2nd argument is greater than the first. i believe im close but still receive my error message even when i have 2 arguments and the second part gives me... (6 Replies)
Discussion started by: livewire06
6 Replies

9. Shell Programming and Scripting

awk syntax question

Hi I use awk command to delete the first blanc line of a file: awk '/^$/ && !f{f=1;next}1' infile > outfile can somebody please explain me what the last "1'" in !f{f=1;next}1' stands for... Thansk a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies

10. UNIX for Dummies Questions & Answers

AWK syntax question

Hi, Have to check file names in some given directory. SO, What is the right syntax here: *$3*=="'$object_list'" - just wanted to check if $3 is in the object_list. And also, Do I need so many quotes around? (5 Replies)
Discussion started by: Leo_NN
5 Replies
Login or Register to Ask a Question