0

I have started a few days ago learning how to make scripts in shell. How can I make script thaht takes two arguments ( file name and some directory), if arguments are bad I want to make error alert and if they are correct I want to copy file in given directory? so far I made this:

#!/bin/bash
if [ -d $1 ] ; then
 ???
 done
else
 echo $1 NOT DIRECTORY
 exit 1
fi

And separate I made a part for checking a file. How to put this together ?

#!/bin/bash
if [ $# -ne 1 ]
then
 echo "Use- $0 file"
 exit 1
fi
if [ -f $1 ]
then
 echo "$1 There is no file with that name"
else
 echo "$1 There is no file with that name"
fi

I am new to bash script so I would like to get some help.

8
  • Welcome! Have you tried something so far? Share your attempts with us. May 6, 2020 at 14:45
  • Have a look at GreyCat and Lhunath's Bash Guide, and make a habit of checking your scripts using shellcheck (also available as standalone tool in many Linux distributions).
    – AdminBee
    May 6, 2020 at 14:50
  • you could have a look here - this is a lession about parameters to shell scripts - also have a look at the other lessions, there may be other information you need.
    – noAnton
    May 6, 2020 at 14:52
  • @schrodigerscatcuriosity First thanks on welcome! So far I have done the part that takes name of directory, and if it is not irectory I have appropriate feedback . I have edited my post. In part ?? I do not know how to take and check file name and make copyS
    – DiN
    May 6, 2020 at 14:53
  • You can just nest the second if construct into the first branch of your first if (note that the done there is wrong; this only applies to for loops). And you would check for $# -ne 2 to ensure two arguments are supplied, then use $2 in the file check. Google for "positional parameters" in order to learn more.
    – AdminBee
    May 6, 2020 at 15:07

1 Answer 1

1

You can use a tructure like this:

  • First check the number of arguments is right, in this case 2

    if arguments not 2
      message "Not enough arguments" and exit
    
  • Check that arguments are valid file and directory.

    if argument1 not a file
      message "Not af file" and exit
    
    if argument2 not a directory
      echo "Not a directory" and exit
    
  • If both arguments are correct, perform the copy:

    message "Copying file"
    copy file to directory
    

Remember to exit in case of error, so the script doesn't keep running.

You can also use one liners instead of the if's:

[[ ! -f "$1" ]] && echo "Not a file" && exit
5
  • In this case you have two conditions, file and directory, so your message could be "bad file or directory" and exit. May 6, 2020 at 15:24
  • Okay, great! So the rest is ok ?
    – DiN
    May 6, 2020 at 15:25
  • Yes, it seems so :) May 6, 2020 at 15:26
  • Great, thanks a lot!
    – DiN
    May 6, 2020 at 15:27
  • You can add the final code to the question, edit it, so everyone else can evaluate it. May 6, 2020 at 15:27

You must log in to answer this question.

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