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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to enter if-then condition on command prompt/line ?
# 1  
Old 02-10-2008
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.
# 2  
Old 02-10-2008
Why are you sourcing a .cshrc and then running sh/ksh type IF command?
What shell are you using?
# 3  
Old 02-10-2008
if-then on command line

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:

Code:
[ -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:
Code:
[ -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.

Last edited by otheus; 02-10-2008 at 11:13 AM..
# 4  
Old 02-10-2008
If you are entering a "simple command" with if, you do not need to enter endif as the following example shows:

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

or you can use \ to continue onto the next line as the following example shows:
Code:
% if ( ! -d backup ) \
? mkdir -p backup
%

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automatically enter input in command line

Hi, This is a script which to create an opvn user, I want which answer automatically to a certain part so, I try this, it works without the red part but I must type manually.. : #!/bin/bash ## Environnement ## LC_ALL=C ## Paths ## rsa_dir="etc/openvpn/easy-rsa"... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

2. Shell Programming and Scripting

Auto enter for prompt messages

Hello everybody, I am coding a script, that allow the user to enter some information using prompt messages, i.e: sEpisode=1 read -e -i "$sEpisode" -p "Start download from episode: " downloadFrom sEpisode="${downloadFrom:-$sEpisode}" This code allows the user to set the download from... (4 Replies)
Discussion started by: Abu Rayane
4 Replies

3. Shell Programming and Scripting

Script to prompt user to enter data

Hi All I have a script that moves files from one dir to another dir based on date, but I would like to change it in a way that whoever is going to run to enter the dates in which files will be removed. This is my script: #!/bin/sh touch -mt 201302250000 /tmp/ref3 touch -mt 201302282359... (14 Replies)
Discussion started by: fretagi
14 Replies

4. Shell Programming and Scripting

Is it possible to prompt for input if not given on command line?

I have a script built that takes the standard inputs $1 $2 $3 after the name and parses some data. hexsite=`echo "obase=16;$1"|bc` hexfix=$(printf "%.3X" 0x$hexsite) if || ;then type=33 elif || ;then type=59 elif ;then type=99 else type=00 fi cat /directory/*.2012$3*| I am... (8 Replies)
Discussion started by: PCGameGuy
8 Replies

5. Shell Programming and Scripting

how to prompt the user to enter an array in tcsh

Hello, I am writing a script that requires the user to enter a string of numbers: ex: 134 345 865 903 This command only allows for one variable to be entered: set "var" = $< and than once I got the array I want to change it to a list with each input on a different line: ... (1 Reply)
Discussion started by: smarones
1 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Solaris

Console - root command prompt displayed twice after hitting enter

Dear All, I hope you can help me. I have a pair of E2900's I've inherited. Both running Solaris 9. Both have LOM> consoles. The problem I'm experiencing only occurs when connected to the /dev/console tty. Whenever I hit 'Enter' for a new line, I receive two new lines: - myhost# ... (11 Replies)
Discussion started by: aleith
11 Replies

8. Shell Programming and Scripting

How to enter commands to the command prompt in a program

Hey, So I'm trying to write a program in unix to automate a process for my astrophysics research. Basically I want the program to prompt the user for some information and store the entered string of text as a variable. I know how to do this. This is where I need help: Now lets say I have a... (4 Replies)
Discussion started by: freemoniez
4 Replies

9. Shell Programming and Scripting

enter password at prompt during a script?

I'm using rsync with the "-e ssh" option so of course it asks for a password using a prompt. Is there a way to tell a script to expect a prompt, wait for it, and give a password when it arrives? There is a way to give rsync a password as part of its options using a file, but it only works with... (2 Replies)
Discussion started by: davidstvz
2 Replies

10. UNIX Desktop Questions & Answers

Website-Command Line Prompt

Hello guys... I am having a doubt. Please try to rectify it. I would really appreciate it. The thing is that is it possible to open any website say for example,google from the command line prompt(terminal) if you are working in Linux-fedora... I am very new to Unix. regards, Mahesh... (2 Replies)
Discussion started by: mraghunandanan
2 Replies
Login or Register to Ask a Question