0

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.

What type of condition should I use? if, case, elseif? Here is my script

!/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

1 Answer 1

0

if, elif and case are all essentially the same thing, they're conditionals.

The difference is stylistic. Technically you could write entire enterprise programs only using case, and not if and elif or the other way around.

Use whatever you'll find the easiest to understand when you come back to your script in 9 months time and have completely forgotten what it does.

6
  • Hahaha. I'll change the case of my CASEs :)
    – Joundill
    Sep 12, 2019 at 21:45
  • Thank you for descriptions, do you have any suggestion for script?
    – Mehrdad
    Sep 12, 2019 at 22:07
  • I think, looking at your issue, I'd use an if rather than a case. Might look prettier if you have to come back to it and change how things work. Particularly if your use-case changes, and you want to loop over all the arguments passed to your script.
    – Joundill
    Sep 12, 2019 at 22:10
  • try that but it always copy file1 and seems one of them [if] execute!
    – Mehrdad
    Sep 13, 2019 at 7:40
  • I modify code please recheck.
    – Mehrdad
    Sep 13, 2019 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .