Condition in bash script

I want get [4 file name string] from user and pass these parameters to bash script. script should copy files in user home directory.

FYI: each file might be exist or not, might be one of them exist or four of them.

Here is my script, it always copy file1 and seems only one of them [if] execute!

#!/bin/bash  
for var in "$@" ; do
    T1=$1
    T2=$2
    if [ $T1 = "File1" ]; then
        cp file1 /home/users
    else      
        if [ $T2 = "File2" ]; then
            cp file2 /home/users
        ....
        fi
    fi 
done

This may help you figure out your logic:

In your 'for' loop:

"var" is going to start with arg1 and get reassigned to the next in the list each iteration until argn
"T1" is always going to be arg1
"T2" is always going to be arg2

Example:

$ cat /tmp/tryit
#!/usr/bin/bash

for a in "$@"; do
  echo $a $1 $2
done
$
$
$ /tmp/tryit a b c d e f
a a b
b a b
c a b
d a b
e a b
f a b
1 Like

Exactly, I get parameter with correct order , but seems if (s) had problems!
Do you have idea about that part of code?

If you're looking to see if the specified file exists

if [ -f <filename> ]; then
  cp ....
else
  cp ...
fi

'-f' test if the file exists, and is a regular file (not a directory, etc)

See 'man test' for more conditions you can test for. They come in handy!

Before cp I cleanup /home/user , and I make sure destination is empty after that run script , do you have any other recommendation?

You lost me there...can you post the entire script, along with how you intend for it to be called?

You could also add a "set -x" line before your 'if' statement so you can see what values it's comparing to see if it's what you expect.

Sorry about confusing you :smile:
After these issues I try to export this part in new and separate script, and just cleanup destinations with rm command and then run if(s)

#!/bin/bash  
rm -rf /home/users
for var in "$@" ; do
    T1=$1
    T2=$2
    if [ $T1 = "File1" ]; then
        cp file1 /home/users
    else      
        if [ $T2 = "File2" ]; then
            cp file2 /home/users
        ....
        fi
    fi 
done