How to enter if-then condition on command prompt/line ?

Hi

I have some trouble entering if-then condition in a single line on a command prompt in csh.
Could someone show how does one do that ?

eg:

source .cshrc;
cd $dir;
pwd;
test -d $backup_dir;
if [ $? -eq 1 ]
then
mkdir -p ${backup_dir};
echo inside loop;
fi;
echo outside loop;
mv -f $data_dir/* $backup_dir;

I keep getting error saying error saying

[sshexec] if: Expression Syntax.
[sshexec] Remote command failed with exit status 1

thank you.

Why are you sourcing a .cshrc and then running sh/ksh type IF command?
What shell are you using?

Ah, you're trying to use sh-style syntax within a csh script.

It's not really possible to do on one line. You can use control expressions to sort of do what you want:

[ -f .profile ] && echo "exists" || echo "does not exist"

Here [ is a symlink to the external program "if" (using if might invoke csh's if). The && and || operate as logical operators; if the test returns a 0 result (is true), the && is executed and the || is short-cutted. However, if the test returns 1 (is false), the && is short-cutted, but the || takes over.

Compound commands within each branch can be done as well:

[ -f .profile ] && echo "exists" || ( touch .profile; echo "exists now!" )

However, the parens probably create a subshell, meaning the environment may not be exactly the same, and of course, it will incur and OS cost. So be careful with this.

If you are entering a "simple command" with if, you do not need to enter endif as the following example shows:

% if ( ! -d $backup_dir ) mkdir -p $backup_dir
%

or you can use \ to continue onto the next line as the following example shows:

% if ( ! -d backup ) \
? mkdir -p backup
%