shell、perl开发示例

shell部分
perl部分

Shell

shell创建空文件

> filename / >> filename
touch filename
echo "" > filename (替换) / echo "" >> filename (补充)

perl

perl IO文件示例
#!/usr/bin/env perl

$infile = shift;  # 获取数组的第一个元素,若未提供则获取 @ARGV 的值
$outfile = shift;
$num_per = shift;
print "参数" , $infile , $outfile , $num_per;   # 打印语句
open IN ,"<:encoding(utf8)" , $infile or die $!;   # < 读取模式,以utf8解码
$cc = 0;  # 定义变量
$file_num = 1;
$line = "";
open OUT , ">:encoding(utf8)", $outfile."/$infile-$file_num" or die $!; # > 写模式

while( $line = <IN> ) # 遍历读取对象内容
{
        $cc++;
        if ($cc%$num_per==1 ) {
                if($cc > $num_per){
                        close OUT;
                        $file_num++;
                        open OUT , ">:encoding(utf8)", $outfile."/$infile-$file_num" or die $!;
                }
        }
        # if ($file_num > 100){
        #       last;     #  last跳出循环,next跳过当前次循环
        # }
        print OUT $line;     # 将读取内容写进新文件中
}

close OUT;  # 关闭文件对象
close IN;
$waves_dir = $ARGV[0];
$in_list = $ARGV[1];
open IL, $in_list;
while ($l = <IL>)
{
        # 去除尾部的换行符
	chomp($l);  
	$full_path = $waves_dir . "\/" . $l;
	$l =~ s/\.wav//; # ~s模式匹配  string=~s/匹配项/替换项/ 这里将 .wav 替换成 nothing
	print "$l $full_path\n";
}

shift文档详解(Perl)

shift ARRAY
shift   Shifts the first value of the array off and returns it,
        shortening the array by 1 and moving everything down. If there
        are no elements in the array, returns the undefined value. If
        ARRAY is omitted, shifts the @_ array within the lexical scope
        of subroutines and formats, and the @ARGV array outside a
        subroutine and also within the lexical scopes established by the
        "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}", "UNITCHECK {}"
        and "END {}" constructs.

        See also "unshift", "push", and "pop". "shift" and "unshift" do
        the same thing to the left end of an array that "pop" and "push"
        do to the right end.

原文链接: https://www.cnblogs.com/lhx9527/p/15796019.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    shell、perl开发示例

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/185342

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 上午10:54
下一篇 2023年2月12日 上午10:54

相关推荐