我將一個csh腳本移植到Perl。我在Perl中做一個switch語句。我不確定這是否正確,基於Perl中不再使用switch語句的不同意見。如果這是正確的,你可以給我一個想法嗎? 同樣在switch語句中,我們使用'when'還是'case'?如何將switch語句轉換爲Perl
這是CSH代碼:
set machine = c16991
set pgMachine = lc0140
if (! -e /abc/site/home/$USER/.userauthentication) then
echo "-F- .userauthentication file must be created in /abc/site/home/$USER "
echo "-I- .userauthentication file format: <emailaddress> <unix pwd>.
echo "-I- Please make sure /abc/site/home/$USER/.userauthentication permission is set to 000"
exit
endif
set permissionCheck = `ls -ltra /abc/site/home/$USER/.userauthentication | awk '{print $1}' |
if ($permissionCheck != 'DASHrwDASHDASHDASHDASHDASHDASHDASH') then
echo "-F- /abc/site/home/$USER/.userauthentication permission is set to $permission1"
exit
endif
@ i = 1
while ($i <= $#argv)
switch ($argv[$i])
case -block:
shift
set DBB1 = $argv[$i]
shift
breaksw
case -tag:
shift
set tag = $argv[$i]
shift
breaksw
case -local:
shift
set local = $argv[$i]
shift
breaksw
case -ar:
shift
set arType = $argv[$i]
shift
breaksw
default:
echo "-E- Invalid switch -> {$argv[$i]} found!"
goto usage
exit
endsw
end
if ($local == 'y') then
if ($tag == "")then
echo "-F- Please enter tag value to proceed!"
exit
endif
endif
set DBB1 = $DBB
### grab data locally
set shipLogFile = "$WARD/ship/log/${DBB1}.ship.log"
set shipUsername = `grep "Username:" $shipLogFile | sed 's/.*Username: //' | sed 's/;.*//'`
set reviewCloseFile = "$WARD/ship/ip/${DBB1}/swizzled/review/${DBB1}.close"
set accept10SumFile = "$WARD/ship/ip/${DBB1}/swizzled/pds/logs/${DBB1}.ccdo_accept10.iss.log.sum"
set shipDate = `zgrep "::RUNTIME:: SHIP end time/date:" $shipLogFile | sed 's/.*time\/date\://g' | sed 's#"##g'`
else
###grab data from archive [DEFAULT]
if ($shipTag == "") then
if ($DBB1 == "") then
# grab DEFAULT hip value from running WARD, $DBB
set DBB1 = $DBB
endif
# grab DEFAULT ship tag value for archive from the latest tag
set shipTag = `ls -t $PROJ_ARCHIVE/noa/${DBB1}/ip_handoff_noa | grep "^$STEPPING" | grep RTL | grep -v RTL0 | grep -v "_TEMP" | head -n 1`
else
if ($DBB1 == "") then
# grab DEFAULT hip value from running WARD, $DBB
set DBB1 = $DBB
endif
endif
set shipUsername = `zgrep "User Name:" $PROJ_ARCHIVE/noa/${DBB1}/ip_handoff_noa/$shipTag/${DBB1}.ip_handoff_noa.manifes t.gz | sed 's/.*\.//g' | sed 's/^ \+\| \+$//g'`
set shipLogFile = "$PROJ_ARCHIVE/noa/${DBB1}/ship_noa/${shipTag}/ship/log/${DBB1}.ship.log"
set reviewCloseFile = "$PROJ_ARCHIVE/noa/${DBB1}/iphandoff_review_noa/${shipTag}/review/${DBB1}.close. gz"
set accept10SumFile = "$PROJ_ARCHIVE/noa/${DBB1}/ipqa_noa/${shipTag}/pds/logs/${DBB1}.ccdo_accept10.is s.log.sum.gz"
set shipDate = `zgrep "Current Date:" $PROJ_ARCHIVE/noa/${DBB1}/ship_noa/${shipTag}/${DBB1}.ship_noa.manifest.gz | sed 's/.*\. //g'`
endif
### create /tmp/transpose_$$.pl script
touch /tmp/transpose_$$.pl; rm /tmp/transpose_$$.pl
echo '#\!/usr/intel/pkgs/perl/5.8.5/bin/perl -w' >> /tmp/transpose_$$.pl
echo 'use strict;' >> /tmp/transpose_$$.pl
echo 'use English;' >> /tmp/transpose_$$.pl
echo '(our $PROG_NAME = $0) =~ s#^.*/##;' >> /tmp/transpose_$$.pl
echo 'my $file = shift;' >> /tmp/transpose_$$.pl
echo 'open (FILE, $file) or die "***E: Error opening $file for reading: $!\n";' >> /tmp/transpose_$$.pl
echo 'my @lines;' >> /tmp/transpose_$$.pl
echo 'while (<FILE>){' >> /tmp/transpose_$$.pl
echo ' chomp $_;' >> /tmp/transpose_$$.pl
echo ' push (@lines, $_);' >> /tmp/transpose_$$.pl
echo '}' >> /tmp/transpose_$$.pl
echo 'print "@lines";' >> /tmp/transpose_$$.pl
echo 'print "\n";' >> /tmp/transpose_$$.pl
echo '1;' >> /tmp/transpose_$$.pl
chmod 740 /tmp/transpose_$$.pl
這是Perl代碼:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper; ##print Dumper()
use feature qw(switch);
my $machine = c16991;
my $pgMachine =lc0140;
if (! -e /abc/site/home/$USER/.userauthentication)
system (echo "-F- .userauthentication file must be created in /abc/site/home/$USER")
system (echo "-I- .userauthentication file format: <emailaddress> <unix pwd>.")
system (echo "-I- Please make sure /abc/site/home/$USER/.userauthentication permission is set to 000")
exit
endif
my $permissionCheck = `ls -ltra /nfs/site/home/$USER/.userauthentication | awk '{print $1}'
if ($permissionCheck != 'DASHrwDASHDASHDASHDASHDASHDASHDASH')
system(echo "-F- /abc/site/home/$USER/.userauthentication permission is set to $permission1")
exit
endif
@ i = 1
given ($i <= $#argv)
switch ($argv[$i])
when(block):
return $argv[$i]
when(tag):
return $argv[$i]
when (local):
return $argv[$i]
when (ar):
return $argv[$i]
default:
system(echo "-E- Invalid switch -> {$argv[$i]} found!")
goto usage
exit
endsw
end
的'之開關功能是好的。但是有一些語法錯誤。 – simbabque
使用[Getopt :: Long](http://p3rl.org/Getopt::Long)來處理命令行參數。 – choroba
而不是'system'echo'',你可以'打印'或'說'。 – choroba