-1

I am getting unexpected end of file error when use using source command to source a file.

I know how to use if..else loop in bash, this is a legacy script. The same script works properly if I run it in tcsh shell. Is there any way to make the script work both in bash & tcsh ?

below is my script,

bash>
bash> source del
-bash: del: line 6: syntax error: unexpected end of file
bash>
bash> cat del
if ( `uname` == Linux ) then
    echo "One"
else
    echo "Two"
endif
bash>

I have already tried all the below solutions but none of them worked..

Link 1

Link 2

Link 3

Link 4

Link 5

Link 6

Link 7

Any reference on this would also be of great help !

ArigatoManga
  • 594
  • 5
  • 23
  • 2
    bash and tcsh provide different languages. You can't have bash run the code of a tcsh script. If you want to source a file in bash, that file must be written in the language tha bash understands. – nos Jul 19 '18 at 11:18
  • in bash it is `if ; then... else... fi` – PTRK Jul 19 '18 at 11:19
  • @PTRK yes i know the if..else in bash but this is a legacy script and this is not working only in bash..is there any way to make it work in both bash & tcsh ? – ArigatoManga Jul 19 '18 at 11:22
  • This script do not work at `bash` shell, the `if` estructure end with `fi`, see this [link](http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php) – Tuxpilgrim Jul 19 '18 at 11:24
  • If you need use this in `bash` and `tcsh`, you can add a condition for shell type, something like: `if < echo $SHELL == /bin/bash >` – Tuxpilgrim Jul 19 '18 at 11:28
  • as @nos said, bash will only understand bash code. Since tcsh and bash are different, I wont think you can achieve your goal. – PTRK Jul 19 '18 at 11:35
  • 3
    In general, you probably cannot write a full script that behaves the same in `bash` and `tcsh`. For this snippet, you *could* write ``test `uname` = Linux && echo One || echo Two``. Here, `&&` and `||` are the only two pieces of shell syntax used (`test` is an ordinary external command), and they are supported by both shells. (However, `... && ... || ...` will not always work as a replacement for an `if` statement.) – chepner Jul 19 '18 at 11:44

3 Answers3

1

Please go through this link

In bash script the correct syntax for if....else is

if [ condition ]
then
        //block 1
else
       //block 2
fi

You can change your code to

if [ $(uname) == Linux ]
then
    echo "ONE"
else
    echo "TWO"
fi

It works fine

SIDHARTH P U
  • 282
  • 1
  • 14
0

It seems it's a (t)csh source. Please try tcsh del (or csh del). Of course you should have installed csh (or tcsh).

uzsolt
  • 5,832
  • 2
  • 20
  • 32
-1

Correct you script like below. Your if condition is not correctly closed.

system=$(uname)
if [[ $system == "Linux" ]]; then
   echo "One"
else
   echo "Two"
fi
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • Your `if` is not correct and would produce an error message. Substitute it by `if [[ \`uname\` == Linux ]]`. Also, since we know that the context is bash, it's besser to not use the obsolete backquotes, but write `if [[ $(uname) == Linux ]]`. – user1934428 Jul 19 '18 at 11:26
  • @user1934428, thanks for your advice. I have already corrected my answer – Abhijit Pritam Dutta Jul 19 '18 at 11:29