-1

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
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Alba
  • 5
  • 4

2 Answers2

0

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

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • I thought that / represent a arithmetical expressoin whereas it's just the path thanks – Alba Oct 25 '15 at 16:44
0
./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
Noproblem
  • 745
  • 1
  • 6
  • 15
  • Thanks a lot for your help . Now I understand where was my problem , / just represent the path. – Alba Oct 25 '15 at 16:42