簡體   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