简体   繁体   English

我正在尝试编写一个bash脚本,但我没有在终端中显示结果

[英]i'm trying to write a bash script and i don't get the result displayed in the terminal

i'm trying to display the 3rd to the 7th line from a file but i get nothing displayed in the terminal i'm using this command : 我正在尝试从文件中显示第3行到第7行但是我在终端中没有显示任何内容我正在使用此命令:

head -n 7 /etc/passwd | tail -n +3

i want the result be seen in the terminal . 我想在终端看到结果。

You can try this way 你可以试试这种方式

head -n 7 /etc/passwd | tail -n 5

For example : 例如 :

seq 20 | head -n 7 | tail -n 5

Output : 输出:

3
4
5
6
7

Explanation : 说明:

head -n 7       -- print the  first 7 lines ( so 1..7 printed)
tail -n 5       -- print last 5 lines ( so skipped first two lines 3..7 printed )
head -n 7 /etc/passwd | tail -n +3

Seems to do exactly what you want it to do for me. 似乎完全按照你的意愿为我做。 Have you verified the contents of /etc/passwd? 你验证了/ etc / passwd的内容吗? If you do not have permission to read the file or it is empty you will get no output. 如果您没有读取文件的权限或者它是空的,则不会输出任何内容。

I would check other places in the script. 我会检查脚本中的其他地方。 Change the first line of your script to: 将脚本的第一行更改为:

#!/bin/bash -v

to have it echo the commands so you can make sure what you think is being executed actually is. 让它回显命令,这样你就可以确定你认为实际执行的是什么。

What do you get than ? 你得到了什么? Show the file output, but choose another file ;) 显示文件输出,但选择另一个文件;)

It sometimes helps to just change commands, try with either HEAD or TAIL twice, to come to the same result. 有时只需更改命令,尝试使用HEAD或TAIL两次,即可获得相同的结果。

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

相关问题 如果没有得到期望的整数值,如何终止脚本? -BASH Shell脚本 - How do i terminate my script if i don't get an expected integer value? - BASH Shell Script Bash:在其他脚本中运行脚本会引发错误,我无法分别运行它们 - Bash: Running scripts inside other script throws errors i don't get running them separately 我不明白 Bash 脚本中“$x*”中的 * 是什么 - I don't understand what the * in "$x*" in my Bash Script 当我在终端窗口的 Linux Mint 上编译具有 getchar() 等的程序时,我没有得到输出 - I don't get an output when I compile a program having getchar(), etc on Linux Mint on the terminal window 获取bash脚本以打开终端 - Get bash script to open terminal 我正在尝试编写一个简单的Lua脚本,并且time函数给了我一个“正在加载模块”错误 - I'm trying to write a simple Lua script, and the time function is giving me a “loading module” error 我无法运行我的bash脚本 - I can't get my bash script to run Bash替换字符串我不想要 - Bash replaceing string i don't want it to 我试图阻止在 BASH 中执行命令.. 并不总是有效 - I'm trying to prevent the execution of a command in BASH.. Doesn't work always 当我在当前 Bash 脚本中调用另一个 Bash 脚本时无法获得预期的输出 - Can't get expected output when I invoke another Bash script in current Bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM