小编Hen*_*eld的帖子

从shell脚本中的另一个文件读取命令?

看我想source在我的shell脚本中使用命令.现在,当我在终端上键入源时,它显示为

-bash: source: filename argument required
source: usage: source filename [arguments]
Run Code Online (Sandbox Code Playgroud)

现在当我在我的shell脚本中使用这个时

#!/bin/sh
source
Run Code Online (Sandbox Code Playgroud)

并保存为test.sh

然后运行就像这样

./test.sh: 2: source: not found
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

linux shell

0
推荐指数
1
解决办法
1806
查看次数

如何在Unix中使用awk而不使用分隔符从文件中提取字段

我有一个文件调用"文件",其中有一些字段用分隔符分隔.该文件的结构是:

@field1@field2@field3@field4
Run Code Online (Sandbox Code Playgroud)

使用awk我已经提取了没有分隔符的字段.

vr=$file;

sep_mx=`echo $vr | awk '{
    n=split($0,x,"@");
    print n
}'` 
echo $sep_mx
## here the number of substring produced is calc.

while ((i<=$max)); 
do
    # would print individual substring one at a
    # time as the counter increases.
    echo $vr | awk -v er=$i '{
        n=split($0,x,"@"); print x[er]
    }'
    ((i+=1))
done
Run Code Online (Sandbox Code Playgroud)

输出是:

field1
field2
field3
field4
Run Code Online (Sandbox Code Playgroud)

如果我想从代码中仅提取第二个字段,我刚刚发布了如何做到这一点?谢谢

unix shell awk ksh separator

0
推荐指数
1
解决办法
1130
查看次数

使用断言控制流程

我想检查一个单词是否有尾随分号。我有一个长字符串中单词的位置,我想检查该位置的字符是否start of word + length of word:。该单词可能是字符串中的最后一个单词,因此尝试获取下一个字符将引发IndexException

我有三个想法:

1)检查我们是否不在字符串的末尾,然后检查它是否不是分号

semicolon_pos = pos_word + len(word) # Possible position
if semicolon_pos < len(long_string) and long_string[semicolon_pos] == ':':
    # do something...
else:
    # do something else
Run Code Online (Sandbox Code Playgroud)

不是通常认为的 pythonic

2) Try- except 分号提取和分号的等式

semicolon_pos = pos_word + len(word) # Possible position
try:
   if long_string[semicolon_pos] == ':':
      # Do something
   else:
      # do something else (which is the same as in the except clause)

except IndexError:
    # do something else …
Run Code Online (Sandbox Code Playgroud)

python exception control-flow

0
推荐指数
1
解决办法
889
查看次数

我如何修复第42行:[:Linux中的参数太多了?

我有1个脚本开头 #!/bin/ksh

这是创建第42行的代码段:[:参数太多了

if  [ -f "$Log_dir/output.rej" ] && 
    [  grep -Hne "fails to validate" $Log_dir/output.rej >/dev/null ]
Run Code Online (Sandbox Code Playgroud)

它扩展为(set -xv)

+ '[' -f /export/home/xxxx/xsdlog/output.rej ']'
+ '[' grep -Hne 'fails to validate' /export/home/xxxx/xsdlog/output.rej ']'
TEST_VALIDATE.sh: line 42: [: too many arguments
Run Code Online (Sandbox Code Playgroud)

我试过了

if  [ -f $Log_dir/output.rej ] &&
    [  grep -Hne "fails to validate"   $Log_dir/output.rej >/dev/null ]
Run Code Online (Sandbox Code Playgroud)

没有引号(").结果相同.

我该如何解决这个错误?

unix linux shell ksh

0
推荐指数
1
解决办法
1149
查看次数

标签 统计

shell ×3

ksh ×2

linux ×2

unix ×2

awk ×1

control-flow ×1

exception ×1

python ×1

separator ×1