shell/awk 脚本在 ubuntu/mint 上工作正常,但在 Red hat 上无法正常工作
我是 Linux 及其 shell 编程的新手。
我在 ubuntu 和 linux mint 上编写了一个 shell 脚本,它在他们的系统上运行得很好。
现在,当我尝试在 red hat 上运行这个 shell 脚本时,整个输出很奇怪。
问题:
\n 字符按原样打印,没有新行打印。
出现一些错误,例如
<前><代码>1。如果 [“$proto”=“TCP”] 2.如果[“$prot_no”=“06”] 3.如果[“$i”-eq 32]expr:non-integer argument
。我只使用了三种类型的语句进行比较 -
我不知道错误到底在哪里,但确实存在ubuntu 和 mint 系统上没有错误。
我听说 ubuntu 中的默认 shell 是 bash,而 Red hat 中的默认 shell 是 ksh,但我对此不确定。
如果是这种情况,那么我如何更改我的 shell,或者如果可能的话,是否有任何命令可以通过 bash shell 运行我的脚本,这样就不需要在脚本中进行任何更改。
请帮助我.... 更改脚本中的所有 \n
字符几乎是不可能的。
编辑:
对于
\n
问题,这是一个示例行..echo "\n\n$proto 标头"
对于其他错误,这里是一些可能有帮助的代码...
if [ "$trans_or_tunn" -eq 1 ] 然后 prot_no=`head -n 1 解密.txt | awk -F " " '{printf "%s",$7}'` 如果[“$prot_no”=“06”] 然后 原型=“TCP” 否则如果[“$prot_no”=“11”] 然后 原型=“UDP” 否则如果[“$prot_no”=“3a”] 然后 原型=“ICMP” 菲 菲 ……
echo "\n\n$proto Header"
这行应该打印三个值之一..TCP HEADER
、UDP HEADER
或ICMP HEADER
。
但屏幕上实际打印出来的内容是..
以上行在屏幕上输出 \n\n标题
I am new to linux and its shell programming.
I wrote a shell script on ubuntu and linux mint which is working perfectly on their systems.
Now when I tried to run this shell script on red hat , whole output is weird.
Problem:
\n characters are printed as it is , no new line is being printed.
some errors occuring like
expr:non-integer argument
.I have used only three types of statements for comparison -1. if [ "$proto" = "TCP" ] 2. if [ "$prot_no" = "06" ] 3. if [ "$i" -eq 32 ]
I don't know where exactly is the error but there was no error on ubuntu and mint systems.
I have heard that Default shell in ubuntu is bash
while in red hat is ksh
but I am not sure about this.
If this is the case then how can I change my shell or if possible is there any command by which I can run my script through bash shell so that there would not be any need to make any change in the script.
Please help me .... It is almost impossible to change all \n
characters in the script.
Edit:
for
\n
issue this is a sample line ..echo "\n\n$proto Header"
for other errors here is some code which may possibly help ...
if [ "$trans_or_tunn" -eq 1 ] then prot_no=`head -n 1 decrypt.txt | awk -F " " '{printf "%s",$7}'` if [ "$prot_no" = "06" ] then proto="TCP" else if [ "$prot_no" = "11" ] then proto="UDP" else if [ "$prot_no" = "3a" ] then proto="ICMP" fi fi .....
echo "\n\n$proto Header"
this line was supposed to print one of the three values ..TCP HEADER
,UDP HEADER
orICMP HEADER
.
but what is actually printed out on screen is ..
Output on screen for above line\n\nHeader
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将脚本的第一行从
使用
echo -e
获取echo
打印\n
作为换行符。使用
[[ ]]
而不是[ ]
进行测试。说明:Ubuntu 上的默认 shell 是
dash
,而不是bash
。这是一个相当有限(但因此速度很快)的 shell,遗憾的是并非在所有 Linux 平台上都可用。上次我检查时,Red Hat 上的默认 shell 是bash
,而不是ksh
,并且几乎在任何地方都可用,因此可移植的 shell 脚本需要要么针对该 shell,要么将脚本写入到该 shellbash
、dash
和ksh
的公共子集。 (如果您感兴趣,最好的可移植子集大致是 中定义的POSIX 标准 shell 命令语言。)Change the first line of the script from
to
Use
echo -e
to getecho
to print\n
as a newline.Use
[[ ]]
instead of[ ]
for tests.Explanation: the default shell on Ubuntu is
dash
, notbash
. This is a rather limited (but therefore fast) shell, that is unfortunately not available on all Linux platforms.bash
, notksh
, was the default shell on Red Hat the last time I checked, and is available almost everywhere, so portable shell scripting requires either targeting that or writing scripts to a common subset ofbash
,dash
andksh
. (If you're interesting, the best portable subset is roughly that defined in the POSIX standard shell command language.)您可以更改所有 \n 字符,例如:
如果文件传输有问题(通过 Windows(winspc...等,已知行结尾问题)
这可能对您有帮助:
您可以调用bash来确保您正在运行bash(如果它安装在所需的操作系统中)。
You can change all \n characters like:
If you have a problem with file transfer (through windows (winspc...etc, known line endings issue)
This might help you:
you can invoke bash to make sure you're running bash (if it's installed in desired OS).
要启用反斜杠转义序列的自动扩展,您可以将此行添加到脚本中的解释器行之后:
“bash”联机帮助页提供了进一步的见解。
To enable automatic expansion of backslash-escape sequences you can add this line to your script after the interpreter line:
The 'bash' manpages offer further insight.
printf
比echo
更便携,并且换行符也能正常工作:至于
if [ "$i" -eq 32 ]
,<代码>$i?错误消息提到
expr
- 您是否在脚本中调用该程序?printf
is more portable thanecho
, and newlines work properly:As for
if [ "$i" -eq 32 ]
, what is in$i
?The error message mentions
expr
-- are you calling that program in your script?