Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 与’;将命令输出到终端_Linux_Bash_Ubuntu_Unix - Fatal编程技术网

Linux 与’;将命令输出到终端

Linux 与’;将命令输出到终端,linux,bash,ubuntu,unix,Linux,Bash,Ubuntu,Unix,一个shell脚本,将命令的输出回显到终端,到目前为止,它只输出空空间。我怎样才能得到想要的结果?我已经尝试在没有任何运气的情况下回显变量本身,所以我猜问题在于命令本身 实际结果: ~$ 较好的结果: Name N. Name Name N. Name Name N. Name Name N. Name 源代码,附带一些注释: #! /bin/bash function ag_students() { # grep -i ^[A-G] -> grep -i [a-g] # it i

一个shell脚本,将命令的输出回显到终端,到目前为止,它只输出空空间。我怎样才能得到想要的结果?我已经尝试在没有任何运气的情况下回显变量本身,所以我猜问题在于命令本身

实际结果:

~$ 
较好的结果:

Name N. Name
Name N. Name
Name N. Name
Name N. Name

源代码,附带一些注释:

#! /bin/bash

function ag_students() {
# grep -i ^[A-G] -> grep -i [a-g] # it is absolutely same but you do not need to hit your [shift] key repeatedly.
# <command> | cut  -d:  -f5  /etc/passwd
# It is stange for me. From the one point of view you want to handle the output
# of the <command1> using <command2> ("cut" utility).
# From the other side you specified the input file for it (/etc/passwd)
# Look at the "cut --help"
# Thus it should be:
# grep -i ^[a-g] /etc/passwd | cut -d: -f5
# without any additions
# Next: "<command> > <name>" will store its output to the file <name>, not to the variable <name>
# "cut -d: -f5 /etc/passwd > tmp" will store its output to the file "tmp", not to the variable with same name
# finally, because, according above, variable "$tmp" still not initialized,
# "echo "$tmp"" will return the empty string whith is assigned to "ag_names" variable

# ag_names=$(grep -i ^[A-G] /etc/passwd | cut -d: -f5 /etc/passwd > tmp | echo "$tmp" ;)

# The right solution:
ag_names=$(grep -i [a-g] /etc/passwd | cut -d: -f5)
# or simpy
grep -i [a-g] /etc/passwd | cut -d: -f5

# It is unnecessary because previous bunch of command alredy output what you want
# echo "$tmp"
}

# Using dollar sign you are trying to execute command contains in the "ag_students" variable (which is not initialized/empty)
# and executing something empty you will get empty result also
$ag_students
# to execute a function - execute it as any other command:
ag_students
# And if you want to have its result in some variable:
tmp=$(ag_students) # as usual
echo "Here are the names: $tmp"

你真的需要深入了解(ba)sh中的管道、重定向和变量用法。你的代码中有很多错误,完整的答案应该是shell使用的完整过程,但不适合这里。PS:尝试从这里开始:
grep-i^[a-g]/etc/passwd | cut-d:-f5
命令工作,我不确定你说的从这里开始是什么意思?我的意思是:在你尝试做类似
var=$(cmd1 | cmd2 | cmd3)
的事情之前,请分别尝试它们,看看你得到了什么。好的,我会尽力写出完整的答案。耐心点。
#! /bin/bash

function ag_students() {
# grep -i ^[A-G] -> grep -i [a-g] # it is absolutely same but you do not need to hit your [shift] key repeatedly.
# <command> | cut  -d:  -f5  /etc/passwd
# It is stange for me. From the one point of view you want to handle the output
# of the <command1> using <command2> ("cut" utility).
# From the other side you specified the input file for it (/etc/passwd)
# Look at the "cut --help"
# Thus it should be:
# grep -i ^[a-g] /etc/passwd | cut -d: -f5
# without any additions
# Next: "<command> > <name>" will store its output to the file <name>, not to the variable <name>
# "cut -d: -f5 /etc/passwd > tmp" will store its output to the file "tmp", not to the variable with same name
# finally, because, according above, variable "$tmp" still not initialized,
# "echo "$tmp"" will return the empty string whith is assigned to "ag_names" variable

# ag_names=$(grep -i ^[A-G] /etc/passwd | cut -d: -f5 /etc/passwd > tmp | echo "$tmp" ;)

# The right solution:
ag_names=$(grep -i [a-g] /etc/passwd | cut -d: -f5)
# or simpy
grep -i [a-g] /etc/passwd | cut -d: -f5

# It is unnecessary because previous bunch of command alredy output what you want
# echo "$tmp"
}

# Using dollar sign you are trying to execute command contains in the "ag_students" variable (which is not initialized/empty)
# and executing something empty you will get empty result also
$ag_students
# to execute a function - execute it as any other command:
ag_students
# And if you want to have its result in some variable:
tmp=$(ag_students) # as usual
echo "Here are the names: $tmp"
#!/bin/bash

function ag_students() {
    grep -i ^[a-g] /etc/passwd | cut -d: -f5
}

tmp=$(ag_students)
echo "Here are the names: $tmp"