执行shell脚本的n行

时间:2022-04-10 01:12:18

Is there a way to execute only a specified number of lines from a shell script? I will try copying them with head and putting them on a separate .sh, but I wonder if there's a shortcut...

是否有一种方法只能从shell脚本中执行指定的行数?我要试着用头把它们复印一下,然后把它们分开。嘘,但是我想知道有没有捷径……

4 个解决方案

#1


2  

Reorganize the shell script and create functions.

重新组织shell脚本并创建函数。

Seriously, put every line of code into a function.

认真地说,把每一行代码都放到一个函数中。

Then (using ksh as an example), source the script with "." into an interactive shell.

然后(以ksh为例),使用“.”作为脚本的源代码,将其转换为交互式shell。

You can now run any of the functions by name, and only the code within that function will run.

现在可以按名称运行任何函数,只运行该函数内的代码。

The following trivial example illustrates this. You can use this in two ways: 1) Link the script so you can call it by the name of one of the functions. 2) Source the script (with . script.sh) and you can then reuse the functions elsewhere.

下面的小例子说明了这一点。你可以用两种方法来使用它:1)链接脚本,这样你就可以用其中一个函数的名称来调用它。2)编写脚本的源代码。然后您可以在其他地方重用这些函数。

function one {
     print one
}

function two {
     print two
}

(
    progname=${0##*/}
    case $progname in
    (one|two)
         $progname $@
    esac

)

#2


1  

Write your own script /tmp/headexecute for example

例如,编写自己的脚本/tmp/headexecute

#!/bin/ksh
trap 'rm -f /tmp/somefile' 0
head -n $2 $1 > /tmp/somefile
chmod 755 /tmp/somefile
/tmp/somefile

call it with the name of the files and the number of lines to execute

用文件的名称和要执行的行数调用它

/tmp/headexecute /tmp/originalscript 10

/ tmp / headexecute / tmp / originalscript 10

#3


0  

Most shells have no such facility. You will have to do it the hard way.

大多数炮弹都没有这种装置。你必须用艰苦的方法去做。

#4


0  

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed -n '1{h;d};H;2{x;s/.*/&/ep;q}' script

This executes the first two lines of a script.

这将执行脚本的前两行。

#1


2  

Reorganize the shell script and create functions.

重新组织shell脚本并创建函数。

Seriously, put every line of code into a function.

认真地说,把每一行代码都放到一个函数中。

Then (using ksh as an example), source the script with "." into an interactive shell.

然后(以ksh为例),使用“.”作为脚本的源代码,将其转换为交互式shell。

You can now run any of the functions by name, and only the code within that function will run.

现在可以按名称运行任何函数,只运行该函数内的代码。

The following trivial example illustrates this. You can use this in two ways: 1) Link the script so you can call it by the name of one of the functions. 2) Source the script (with . script.sh) and you can then reuse the functions elsewhere.

下面的小例子说明了这一点。你可以用两种方法来使用它:1)链接脚本,这样你就可以用其中一个函数的名称来调用它。2)编写脚本的源代码。然后您可以在其他地方重用这些函数。

function one {
     print one
}

function two {
     print two
}

(
    progname=${0##*/}
    case $progname in
    (one|two)
         $progname $@
    esac

)

#2


1  

Write your own script /tmp/headexecute for example

例如,编写自己的脚本/tmp/headexecute

#!/bin/ksh
trap 'rm -f /tmp/somefile' 0
head -n $2 $1 > /tmp/somefile
chmod 755 /tmp/somefile
/tmp/somefile

call it with the name of the files and the number of lines to execute

用文件的名称和要执行的行数调用它

/tmp/headexecute /tmp/originalscript 10

/ tmp / headexecute / tmp / originalscript 10

#3


0  

Most shells have no such facility. You will have to do it the hard way.

大多数炮弹都没有这种装置。你必须用艰苦的方法去做。

#4


0  

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed -n '1{h;d};H;2{x;s/.*/&/ep;q}' script

This executes the first two lines of a script.

这将执行脚本的前两行。