结合bourne shell和awk

用户名

这是我的实际文件WASfile

#!/bin/sh
sed -i '/^ *$/d' WASfile  
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasdm/WASfile;  
Script to Add  
read -p "Press [Enter] to continue for Installation"  

现在,我想将以下脚本放在“要添加的脚本”上方,然后将WASfile作为脚本执行(这是许多脚本或命令的组合)。

#!/usr/bin/awk -f  

BEGIN   { FS="=" }  
NR==FNR { a[$1]=$0; next }  
$1 in a { $0=a[$1] }  
/^#/    { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }  
1    

我想按以下方式组合和使用,或者将两种脚本组合在一起的更好方法。

#!/bin/sh  
sed -i '/^ *$/d' WASfile  
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasdm/WASfile;  
#!/usr/bin/awk -f  

BEGIN   { FS="=" }  
NR==FNR { a[$1]=$0; next }  
$1 in a { $0=a[$1] }  
/^#/    { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }  
1  
read -p "Press [Enter] to continue for Installation"  

我无法执行脚本。

因此,我尝试将AWKscript提取到另一个文件并尝试执行该AWKscript。但是,问题是提取后,主脚本WASfile本身会损坏或失败。

#!/bin/sh
sed -i '/^ *$/d' WASfile;
sed -i -e '/\/usr\/bin\/awk/,/baba/{w 1' -e 'd}' WASfile;
#!/usr/bin/awk -f

BEGIN   { FS="=" } NR==FNR { a[$1]=$0; next } $1 in a { $0=a[$1] } /^#/    { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } } 1
baba
read -p "Press [Enter] to continue for Installation"

如下

#./WASfile
sed: -e expression #1, char 24: missing command
./WASfile: line 6: BEGIN: command not found
./WASfile: line 7: {: command not found
./WASfile: line 7: next: command not found
./WASfile: line 8: in: command not found
./WASfile: line 9: syntax error near unexpected token `/^#/,'
./WASfile: line 9: `/^#/    { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }  '
帕布克

根据您的问题,我知道您的第一个脚本存储在名为的文件中WASfile您必须确保脚本已设置了可执行位:

chmod a+x WASfile

然后,您可以执行脚本:./WASfile因为PATH默认情况下当前目录不在变量中,所以您必须为当前目录./或绝对路径显式指定路径/home/wasadm/WASfile

这同样适用于AWK脚本:使其可执行并使用指定的路径进行调用。

WASfile脚本中,可以像在命令行中一样调用它。命令行也是shell,与执行firs脚本的shell相同或相似。

#!/bin/sh
sed -i '/^ *$/d' WASfile
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasadm/WASfile
/path/to/the/AWKscript inputfile1 inputfile2 >outputfile1
read -p "Press [Enter] to continue for Installation"

上面的代码将运行AWKscript存储在/path/to/the目录中脚本以文件作为参数并带有自描述名称。将您需要的文件放在此处。

另一种选择是awk显式调用在这种情况下,您无需启用文件的可执行位。

awk -f /path/to/the/AWKscript

问题中的最后一段代码不起作用

最后一段代码中显示的组合不起作用。类Unix的系统被设计为由单个解释器执行单个可执行文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章