The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
prompt line sobnc AIX 0 01-19-2008 05:50 PM
command for deleting log files based on some condition pulkit Shell Programming and Scripting 4 01-09-2008 06:17 AM
inconsistent ls command display at the command prompt & running as a cron job rajranibl SuSE 5 07-30-2007 08:26 AM
Path in prompt line? Leitwolf UNIX for Dummies Questions & Answers 4 04-02-2007 03:25 PM
2 line prompt Kelam_Magnus UNIX for Dummies Questions & Answers 7 06-13-2002 10:23 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-10-2008
mpc8250 mpc8250 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 5
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 (permalink)  
Old 02-10-2008
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
Why are you sourcing a .cshrc and then running sh/ksh type IF command?
What shell are you using?
  #3 (permalink)  
Old 02-10-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
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 (permalink)  
Old 02-10-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
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
%
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0