LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2009, 11:34 AM   #1
tspang
LQ Newbie
 
Registered: May 2009
Posts: 13

Rep: Reputation: 0
Simple? shell questions from a Perl programmer


Hello all, first post.

Due to various reasons, I'm stuck writing some fairly complex scripts in shell (ksh or bash, depending on the system). I have quite a bit of perl experience, but little experience using shell for more than really simple tasks.

If there is some excellent resource that I don't know about that maps common perl procedures to an equavelent shell procedure I'll happily go figure it out on my own. Since I haven't found such a thing, I'll just throw them out here.

Things I'm trying to do in shell, that would be easy in perl that I can't seem to figure out in shell:

1) Take a space deliminated value from awk and assign it to 2 variables.

In perl, I would do something like this:
Code:
$sys_return=`tar -tvf /dev/nst0 | awk '{print $3,$6}'`;
($fileName,$fileSize) = split (/\s/,$sys_return);
In bash, I get stuck doing something like this:
Code:
FNAME=`tar -tvf /dev/nst0 | awk '{print $6}'`
mt -f /dev/nst0 bsfm 1
FSIZE=`tar -tvf /dev/nst0 | awk '{print $3}'`
echo "restoring $FNAME with filesize of $FSIZE"
It must be easier, right?

2) Read a file and look for a value line by line
Perl:
Code:
open(FILE,"list.txt");
foreach $line (<FILE>) {
  $verified = 1 if ($line =~ /$pattern_to_match/);
}
shell:
I'm not even sure, at the moment. I was searching for that when I signed up here. I see some references to this:

Code:
while read line
do
 if [[ $line == *$pattern_to_match* ]]
 then
  #do something
 fi
done
but none of the sample code I can find actually shows how to pick which file you want to read from.

Thanks for assistance in advance, I'm sure I'll have more questions later!
 
Old 05-21-2009, 12:44 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
1)
Code:
set `tar -tvf /dev/nst0 | awk '{print $3,$6}'`
FSIZE=$1
FNAME=$2
The set command makes the input into positional parameters - since there are 2 items of input you get $1 and $2. (Not to be confused with the $1 and $2 awk itself would have seen in the prior step - $1 = $3 from awk and $2 = $6 from awk).

2)
Code:
FILE=yourfile
while read line
do
 if [[ $line == *$pattern_to_match* ]]
 then
  #do something
 fi
done <$FILE
Since it is a while loop you just redirect in from the file at the end of the loop (done). I set a variable before the loop with name of file so that variable can be used at end but that's not required - you could hard code the file name at the redirect.
 
Old 05-22-2009, 01:39 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Loop a bit more Perlish
Code:
IFS="
" # hard coded rtn inside quotes
for rec in `cat $file`
do
    if [[ "$rec" =~ "pattern" ]]   # needs bash >= v3  http://tldp.org/LDP/abs/html/bashver3.html
    then
        do_something
    fi
done
 
Old 05-22-2009, 02:04 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by tspang View Post
In perl, I would do something like this:
Code:
$sys_return=`tar -tvf /dev/nst0 | awk '{print $3,$6}'`;
($fileName,$fileSize) = split (/\s/,$sys_return);
you could do it inside awk
Code:
tar -tvf /dev/nst0 | awk '{
  filename=$6
  filesize=$3
}
/pattern_to_match/{
  #do something
}'
 
Old 05-22-2009, 01:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by tspang View Post
2) Read a file and look for a value line by line
Perl:
Code:
open(FILE,"list.txt");
foreach $line (<FILE>) {
  $verified = 1 if ($line =~ /$pattern_to_match/);
}
I'm surprised nobody mentioned this:
Code:
if grep -q "$pattern_to_match" list.txt ; then
  verified=true # or verified=1 if you prefer
else
  verified=false
fi
 
  


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
New Perl Programmer Doctorzongo Programming 7 09-09-2008 08:10 PM
Programmer needed for simple game. JasonByers Programming 2 04-06-2007 08:08 AM
2 simple shell prog questions provkitir Linux - Software 3 11-23-2004 03:00 AM
Two simple questions...shell scrips and icons vdogvictor Linux - General 4 04-12-2004 05:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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