繁体   English   中英

如何在Shell脚本中具有反引号的另一个变量中调用函数

[英]how to call a function within another variable which has backtick in Shell Script

我想在另一个具有反勾号的变量中调用一个函数。 我该如何工作?

例:

location ()
{echo "home/$1/dir"}

count_files ()
{
count=`ls $(location user1) |wc -l` 
}

count_files

您可以使用:

location() {
   echo "home/$1/dir"
}

count_files() {
   count=$(ls $(location user1) | wc -l)
}

count_files

您必须设置正确的执行语法。

$(Command)

或带有反勾号

`Command`

在您的示例中,您缺少了ls ...命令。 而且您在主目录中缺少第一个斜杠(否则,它只能在根目录/目录中使用。)

尝试:

#!/bin/bash

location () {
  echo "/home/$1/dir"
}

count_files () {

  # Get the count;
  count=$(ls $(location "$1") | wc -l);

  # Return the count value;
  echo "$count";
}

# Call function and print the count result. 
# Better you set the user here and not 'hardcoded' in a function.
echo $(count_files "user1")

暂无
暂无

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

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