Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用bashshelllinux制作USB复制程序_Bash_Shell - Fatal编程技术网

用bashshelllinux制作USB复制程序

用bashshelllinux制作USB复制程序,bash,shell,Bash,Shell,这是我用bashshell将所有fd数据复制到home/user目录的代码 #!/bin/sh user=$(whoami) now=$(date +"%d.%m.%Y_%H.%M.%S") dir=$(mkdir $now) --> and here I want to create a folder as a place to copy the results according to datetime if [ -d /media/$user/foo ] the

这是我用bashshell将所有fd数据复制到
home/user
目录的代码

#!/bin/sh
user=$(whoami)
now=$(date +"%d.%m.%Y_%H.%M.%S")
dir=$(mkdir $now) --> and here I want to create a folder as a place to copy the results according to datetime

if [ -d /media/$user/foo ]
then
    echo "any media"
    cp -r /media/$user /home/$user/$dir
else
    echo "none of media attached"
fi
但是我运行的是代码,
cp-r/media/$user/home/$user/$dir
也没有运行,它不会被复制到基于datetime的新创建的目录中,有什么建议吗?或 我的代码有问题吗?

我建议替换

dir=$(mkdir $now)


如果目录创建出现问题,这将终止您的脚本。

Btw.:
sh
()通常不是
bash
()。或者使用
mkdir-p
。我会尝试的,先生,如果可能的话,我会问是否有办法在后台运行sh程序,这样当检测到usb时,它会自动运行sh程序。我把它放在bashrc文件中,但它只在终端打开时运行。这可能会有所帮助:或者我读过,但我什么都不懂,但谢谢你的建议
mkdir "$now" || exit 1
dir="$now"