Welcome to the Linux Foundation Forum!

Deciphering lines of code

Hey everyone I don't know if this is the right place to ask this question but I am trying to learn what different lines of code do and I need some help. I am looking at the Save command specifically. It was written to save files and back then up to a directory for later backup usage. The problem that I am having is in the first part of the code. For instance on this line

if [! -d $1];then echo $1 is not a directory; exit 1; fi

I know the fi means that the line is finished and the echo is a command to run the $1 file but what is the first part? Any help will be appreciated. Thanks

Comments

  • Posts: 41
    Well fi actually marks the end of the if statement (it's just "if" backwards). The [! -d $1] is performing a test on a passed value, which is being referred to by $1 (the 1st parameter). -d tests to see whether it is a directory. The ! negates the statement to make it check whether it is not a directory.

    In English, it's saying "If the parameter passed is not a directory, then say '[value of parameter passed] is not a directory."

    Here are more options you can use (although probably not a complete list):
    1. -e file
    2. -s non-empty file
    3. -b block device
    4. -c character device
    5. -f a non-directory and non-device file
    6. -p pipe
    7. -h symbolic link
    8. -r file has read permission
    9. -w file has write permission
    10. -x file has execute permission
    11. -k has sticky bit set
    12. -O you are the file owner
    13. -G same group as you
    14. -N modified since last read
  • Posts: 41
    Oh, I should have clarified the square brackets. Those just indicate that a test is being performed inside them, and it will return 0 (true) or 1 (false) to the if clause.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training