问题
What does this mean?
if [ -f $2/$1 ]
and this line:
cp $1 $2/$1
Does $2/$1
represent a file, because it's associated with -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
回答1:
./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
回答2:
Given 2 arguments / names ($2
and $1
), this is testing to ensure that a file under directory $2
with name $1
exists. If so, then it copies a local file indicated by $1
into directory $2
with the same name.
However, per your following example, it is put to use a little differently. If the file doesn't already exist in the destination, it immediately copies a file into a subdirectory with the name specified by $2
. If the file does exist in the destination, it first prompts the user if it is o.k. to overwrite the existing file.
http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
来源:https://stackoverflow.com/questions/33331858/explain-me-2-lines-of-this-shell-script