15
3
I'm trying to write a script that has to check if a file exists. In the console I write
if [[ -a /path/to/file.txt ]]; then echo "not mod"; else echo "mod"; fi
and I get
not mod
but when I write a script to do the same thing:
#!/bin/sh
if [[ -a /path/to/file.txt ]]; then echo "not mod"; else echo "mod"; fi
and then execute the script, I get this:
./ex.sh: 2: [[: not found
mod
I saved the script on the current directory and named it ex.sh, then I made sure it is executable. To call the script I do this:
./ex.sh
Why am I getting this problem? I already tried many things:
if [ -a /home ...
and
if test -a /home ...
Both of them return
13: -a: unexpected operator
I was able to make it work, but for that I had to remove the first line (#!/bin/sh) Do you know why this happens? – Buzu – 2012-01-04T06:21:27.680
try
#!/bin/bash
– kev – 2012-01-04T06:38:57.997hello kev, I relized that too. I just updated my question. I spent too much time on this, but I'm glad I could solve it. Thanks for your answer. It is indeed, the solution to the problem. – Buzu – 2012-01-04T06:43:08.783
@Kev Please post that as an answer – slhck – 2012-01-04T08:42:19.670
Buzu, @Kev - if you find an answer, post it as an answer below, do not edit the question. – user1686 – 2012-01-04T08:43:04.957
@kev: I edited the question to remove the answer, since all the information is already in the answer you posted. – Keith Thompson – 2012-01-04T21:49:02.530