Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing current position in UNIX to get system variables 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I wrote this code

$rc=system("pwd");

open(OUT, ">/usr/local/lawson/temp.txt");

foreach (<$rc>) {
chomp;
my @fields = split ///;
$lawdir = "/" . $field[0] . "/" . $field[1] . "/" . $field[2] . "/";
$sys = $fields[3];
print OUT ("LAWDIR is " . $lawdir), "\n";
print OUT ("sys is " . $sys), "\n";
}

close (OUT);

I am trying to get the value returned from the system command "pwd", and then parsing it. The reply should be: /usr/lawson/lawson/ocft/javasrc

I want to put /usr/lawson/lawson/ into the varaible $lawdir and ocft into the variable $sys.

I am getting an error on the split with the ///.

How do I get around this?
 
Try this:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$pwd_res_str[/blue] = [red]'[/red][purple]/usr/lawson/lawson/ocft/javasrc[/purple][red]'[/red][red];[/red]
[gray][i]# split into array[/i][/gray]
[black][b]my[/b][/black] [blue]@pwd_res_arr[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\/[/b][/purple][/purple][red]/[/red],[blue]$pwd_res_str[/blue][red])[/red][red];[/red]
[gray][i]# join a slice of an array[/i][/gray]
[black][b]my[/b][/black] [blue]$lawdir[/blue] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]'[/red][purple]/[/purple][red]'[/red], [blue]@pwd_res_arr[/blue][red][[/red][fuchsia]0..3[/fuchsia][red]][/red][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]lawdir='[blue]$lawdir[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]
 
This was helpful getting past the parsing issue and I changed the code.

$rc=system("pwd");

open(OUT, ">>/usr/local/lawson/temp.txt");

for (<$rc>) {
my @fields = split /\//;
$lawdir = join('/', @fields[0..3]);
$sys = $fields[3];
print OUT ("LAWDIR is " . $lawdir), "\n";
print OUT ("sys is " . $sys), "\n";
}

close (OUT);

I wanted to use the first line of the code to have the system tell me where I am at. I am trying to get away from hard coding. The system call does not appear to be working.
 
Hi

jcarrott said:
The system call does not appear to be working.
It is certainly working. Read the documentation of [tt]system()[/tt] to know how it is working. Or if you not want to know, just change it to this :
Code:
$rc=[highlight]`[/highlight]pwd[highlight]`[/highlight];

open(OUT, ">>/usr/local/lawson/temp.txt");

my @fields = split /\//[highlight],$rc[/highlight];
$lawdir = join('/', @fields[0..3]);
$sys = $fields[3];
print OUT ("LAWDIR is " . $lawdir), "\n";
print OUT ("sys is " . $sys), "\n";

close (OUT);

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top