[英]bash script not interpreting commands
I am writing a simple bash script (I am fairly new to bash). 我正在编写一个简单的bash脚本(我对bash还是很陌生)。 I am trying to assign the output of a command to a variable and I can't seem to get it to work properly. 我试图将命令的输出分配给变量,但似乎无法使其正常工作。 See below: 见下文:
#!/bin/bash
HOST=`hostname`
MEMORY=`prtconf -v |grep Memory |awk '{print $3}'`
echo "os_instance_name "$HOST
echo "physical_machine_ram "$MEMORY
I am able to get the result of hostname
properly but my MEMORY
variable is not being populated with the output from the command. 我能够正确获取hostname
的结果,但是我的MEMORY
变量未填充命令的输出。
Try this: 尝试这个:
memory=`free -m | grep Mem | awk '{print $2}'`
echo $memory
should return the physical memory in megabytes 应该以兆字节为单位返回物理内存
Also, $1
in the code that you have provided might be referring to a blank column. 另外,您提供的代码中的$1
可能引用空白列。 Try using $2
. 尝试使用$2
。
Some ideas spring to my mind: 我想到了一些想法:
execute the script in debug mode 在调试模式下执行脚本
$ bash -x myscript.sh
Put the variable expansions within the double quotes 将变量展开放在双引号内
echo "os_instance_name $HOST" echo "physical_machine_ram $MEMORY"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.