如何在函数中创建目录

 收藏

编辑:

我正在尝试构建一个简单的功能,该功能可以打印出名称,然后创建目录。我是在bash中构建函数的新手,所以下面的脚本不起作用:

dest_path=/home/all/todo    
line="name"

mkdir_for_name() {
echo $1
mkdir $2
}

mkdir_for_name $name
mkdir_for_name $dest_path/$name

该语法有什么问题?

回复
  • in_id 回复

    您定义了一个接受2个参数的函数,其中传递的第一个参数是要在屏幕上打印的名称(“ echo”行),而传递的第二个参数将创建desidered文件夹。 仅传递一个参数只会在屏幕上显示该参数

  • He猪 回复

    Since you are using echo so I believe you want to print directory name; and off course you need to pass 2 arguments to your function. May be call your function in following way. In this way you need not to change your code.

    mkdir_for_name  "$name"  "$dest_path/$name"
    

    完整的脚本:

    dest_path=/home/all/todo    
    line="name"
    
    mkdir_for_name() {
    echo "$1"
    mkdir "$2"
    }
    
    mkdir_for_name  "$name"  "$dest_path/$name"
    

  • Oo苑 回复

    Your function uses $2, but you're only calling it with one argument, so $2 is empty.