Bash 给我解释一下这个shell脚本的两行代码
这是什么意思Bash 给我解释一下这个shell脚本的两行代码,bash,shell,file-exists,cp,Bash,Shell,File Exists,Cp,这是什么意思 if [ -f $2/$1 ] 这一行: cp $1 $2/$1 $2/$1是否表示文件,因为它与-f关联 #!/bin/bash if test $# -ne 2 then echo "Numbers of argument invalid" else if [ -f $2/$1 ] then echo "file already exist , replace ?" read return if test $return = 'y'
if [ -f $2/$1 ]
这一行:
cp $1 $2/$1
$2/$1
是否表示文件,因为它与-f
关联
#!/bin/bash
if test $# -ne 2
then
echo "Numbers of argument invalid"
else
if [ -f $2/$1 ]
then
echo "file already exist , replace ?"
read return
if test $return = 'y'
then
cp $1 $2/$1
else
exit
fi
else
cp $1 $2/$1
fi
fi
给定两个参数/名称($2
和$1
),这是为了确保目录$2
下存在名为$1
的文件。如果是这样,则它将$1
指示的本地文件复制到同名的$2
目录中
但是,根据下面的示例,它的使用方式略有不同。如果文件在目标中不存在,它会立即将文件复制到子目录中,其名称由$2
指定。如果目标中确实存在该文件,则会首先提示用户是否可以覆盖现有文件
非常感谢您的帮助。现在我知道我的问题出在哪里了,/只代表路径。我认为/代表一个算术表达式,而它只是路径thanksBTW,这条线有问题。把它接过去,了解详情。
./script.sh "tmp.txt" "/project"
echo $1 = tmp.txt
echo $2 = /project
$1 - file name
$2 - directory
if [ -f $2/$1 ] - checks if file exists, -f for files, -d for directory
cp $1 $2/$1 - copy file to directory/file_name
if [ -f /project/tmp.txt ]
cp tmp.txt /project/tmp.txt