简体   繁体   English

diff命令在bash shell脚本中不起作用

[英]diff command not working inside bash shell script

I am trying to run 我正在尝试跑步

diff <(tar -tvf HIVE_CLIENT.tar.gz | sort) <(tar -tvf YARN_CLIENT.tar.gz | sort) 

putting this command inside script, when I execute script it shows error 将此命令放在脚本中,执行脚本时显示错误

syntax error near unexpected token `(' "

But when I do not put inside script rather than run from shell directly it works. 但是当我不放入脚本而不是直接从shell运行时,它就可以工作。

Probably your script is run with /bin/sh and not with /bin/bash , but command substitution is a bash feature and not implemented in sh. 可能您的脚本是使用/bin/sh运行的,而不是使用/bin/bash ,但是命令替换是bash的功能,不能在sh中实现。 So I suppose you are using bash as your shell which is why it is working from the command line. 因此,我想您正在使用bash作为外壳程序,这就是为什么它可以从命令行运行的原因。

Try adding this prefix to your script, and remove existing shebangs (like #!/bin/sh or similar): 尝试将此前缀添加到脚本中,并删除现有的shebang(例如#!/bin/sh或类似名称):

#!/bin/bash

You should try following two actions: 您应该尝试执行以下两个操作:

  1. Use #!/bin/bash as your shebang (First line of your script) 使用#!/bin/bash作为您的shebang(脚本的第一行)

  2. This may be needed based on your bash , use only if opetion 1 does not help. 根据您的bash可能需要这样做,仅在操作1无效时才使用。 Use following commands to flip between posix mode which is needed for process substitution: 使用以下命令在进程替换所需的posix模式之间切换:

     set +o posix diff <(tar -tvf HIVE_CLIENT.tar.gz | sort) <(tar -tvf YARN_CLIENT.tar.gz | sort) set -o posix 

    Example: 例:

     wc -l <(ls -lrt) sh: syntax error near unexpected token `(' set +o posix wc -l <(ls -lrt) 114 /dev/fd/00 set -o posix wc -l <(ls -lrt) sh: syntax error near unexpected token `(' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM