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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unix command in perl

Status
Not open for further replies.
Jul 17, 2003
155
0
0
US
Hi,

I have these lines in perl script:

$filename = "patches" . time . ".html";
$html_file = "/tmp/" . $filename;

Now, I want to append a hostname to the $html_file. I did as follows:

$machine = system("/usr/bin/hostname");
$html_machine = "/tmp/" . $machine;
system("/usr/bin/mv","$html_file","$html_machine");

But it does not work.
I am new to perl.
Thanks for any help

 
The system() function is indeed used for executing shell commands. However it returns the exit code of the command, rather than its output. If you want to grab the output, use backticks or qx// (they function exactly the same) - example:
Code:
my $machine = qx!/usr/bin/hostname!;

#alternatively
my $machine = `/usr/bin/hostname`;
 
It's the most common way of declaring variables in perl. It's a good habit to get into, especially if you go on to make something big. If you don't use it, every variable you use in your programs are global, which can be a nightmare to maintain.

The gory technical details are here:
 
I entered these:

$machine = `/usr/bin/hostname`;
$filename = "$machine" . "patches" . time . ".html";
$html_file = "/tmp/" . $filename;

Now, the output shows:

tahoe?patches1075740504.html

------
tahoe is my machine name, but how do I change the ? in tahoe?patches1075740504.html to tahoe_patches1075740504.html
 
I'd like to backup files before appending new content on a Unix server... I've been using
$backup = 'cp oldfile.txt oldfileBU.txt>'
without knowing why it works. Is there a more correct way to do this that might allow a variable that contains he filename to be used instead of the filename itself?
 
Ok, that'll probably all work, but you can do the whole thing much easier and cleaner.. first off, it looks like you want the current date/time in the filename...do you want it to be the perl seconds format like you have as one big number or would you want the english readable way?

here's a way to get the filename as a english looking string:

($string=localtime)=~s/ /_/g;

and then string would be "Mon_Feb__2_10:35:17_2004"

if you want it more consise I can show you code to give this string :

"2.2.04@10.38am"

(I'll attach the sub at the end and call it "nicedate" and if you call it with the argument "nicetime" it'll put the time on the end as shown).

second, it looks like all you want to do is rename
/tmp/patches1075740504.html
to
/tmp/tahoe_patches1075740504.html
right?


how about just :
Code:
my $timstr=time;      #so that successive calls dont keep changing it...
my $html_file   ="/tmp/patches$timestr.html";
my $html_machine="/tmp/[COLOR=red]$ENV{HOST}[/color]_patches$timestr.html";
`/usr/bin/mv $html_file $html_machine`;

notice I just use the HOST env variable instead of calling the 'hostname' system script...if you cant or dont want to do that just go:

Code:
chomp(my $host=`/usr/bin/hostname`);

note that the "chomp" says to remove the trailing return at the end of the system call...which is probably why you get the "?" at the end of your hostname...



here's the code to get the nicedate (try the two args time and nicetime...see if you like..)

Code:
sub nicedate {
    my $cmd=shift(@_) || "";

    my ($CMD__nicetime,$CMD__time,$ampm,$dateis,$day,$h,$isdst);
    my ($m,$month,$s,$time,$wday,$yday,$year);

    if ($cmd ne "") {
	if    ($cmd =~ /nicetime/i) {$CMD__nicetime=1}
	elsif ($cmd =~ /time/i)     {$CMD__time=1}
    }

    ($s,$m,$h,$day,$month,$year,$wday,$yday,$isdst) = localtime();

    $month++;                # correct for 0-11
    $year += 1900;           # spec is years since 1900..
    ($year)=$year=~/(..)$/;  # 2 digit date is fine..

    if ($month   =~ /^0(.*)/) {$month=$1}
    if ($day     =~ /^0(.*)/) {$day=$1}

    $dateis = "$month.$day.$year";

    if ($CMD__nicetime) {
	if ($h >= 12) {$ampm="pm"} else {$ampm="am"}
	if ($h >  12) {$h-=12}
	if ($h =~ /^0(\d)$/) {$h=$1}
	if ($m =~ /^\d$/) {$m="0$m"}
	$dateis="$dateis\@$h.$m$ampm";
    } elsif ($CMD__time) {
	$time="${h}:${m}:${s}";
	$dateis="$dateis\@$time";
    }
    return "$dateis";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top