0

I need to understand how a script that was given to me works. There are some if conditions and commands and syntaxes I have no idea what they do and I've tried Googling them but had no luck.

  1. So what does [ -z String ] do?

    For example this appears in the script [ -z "$(which ssh)" ]
    Also is $(which ssh) a string or a variable?

  2. What does ~/.filename mean? I know that ./filename is used to execute files sometimes and that ./ its a pointer to the directory itself, but what is the ~/.?

  3. What does [ -e filename ] do?

  4. What does [ ! -d ~/.ssh ] do?

1 Answer 1

1

You can find more about [ by typing man [ into the terminal. This is a test construct for use after if, etc. Hence,

   -z STRING
          the length of STRING is zero
   -e FILE
          FILE exists
   ! EXPRESSION
          EXPRESSION is false
   -d FILE
          FILE exists and is a directory

1) [ -z "$(which ssh)" ] means test if "$(which ssh)" is a zero-length string. $(...) means to execute the ... part first. If you run which ssh in a terminal, you'll see that it will produce the path to the shh executable, if it exists. Hence, if this exists, then the string will not be zero-length, and the test will fail.

2) ~ is a shortcut for your home directory, usually located at /home/your_user_name. [ -e filename ] means test if this file exists, and return true if it does.

3) [ ! -d ~/.ssh ] means test if ~/.ssh (probably /home/your_user_name/.ssh) exists and is a directory, and if it is not, then return true.


==EDIT==

You should try and sort out the man problem, because it'll really advance your bash knowledge. If man [ doesn't work, try man test instead. (test is a synonym for [...].) If not, here is an online version.

   -w FILE
          FILE exists and write permission is granted

I actually think you might have it the wrong way around. It should be

[ -w "$1" ]

not

[ "$1" -w ]
2
  • ive tried using man [ but with no resutls. also googling it. What about [ "$1" -w ] what does -w do to the archive that has route $1?
    – user282724
    May 18, 2014 at 16:52
  • I'll answer in the answer, so I can format.
    – Sparhawk
    May 18, 2014 at 17:01

You must log in to answer this question.

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