将带有通配符(*)的文件复制到bash脚本中的文件夹中-为什么不起作用? [英] Copying files with wildcard (*) to a folder in a bash script - why isn't it working?

查看:35
本文介绍了将带有通配符(*)的文件复制到bash脚本中的文件夹中-为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,该脚本创建一个文件夹,并将文件复制到该文件夹​​.它可以从命令行运行,但不能从我的脚本运行.怎么了?

I am writing a bash script that creates a folder, and copies files to that folder. It works from the command line, but not from my script. What is wrong here?

#! /bin/sh
DIR_NAME=files

ROOT=..
FOOD_DIR=food
FRUITS_DIR=fruits

rm -rf $DIR_NAME
mkdir $DIR_NAME
chmod 755 $DIR_NAME

cp $ROOT/$FOOD_DIR/"*" $DIR_NAME/

我得到:

cp: cannot stat `../food/fruits/*': No such file or directory

推荐答案

您完全倒退了-除 * 字符外的所有字符都应双引号:

You got that exactly backwards -- everything except the * character should be double-quoted:

#!/bin/sh
dir_name=files

root=..
food_dir=food
fruits_dir=fruits

rm -rf "$dir_name"
mkdir "$dir_name"
chmod 755 "$dir_name"

cp "$root/$food_dir/"* "$dir_name/"

此外,根据最佳实践/惯例,非环境变量名称应小写,以免名称与环境变量和内置变量冲突.

Also, as a matter of best-practice / convention, non-environment variable names should be lower case to avoid name conflicts with environment variables and builtins.

这篇关于将带有通配符(*)的文件复制到bash脚本中的文件夹中-为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆