The UNIX and Linux Forums  


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
If conditional abhinavsinha UNIX for Dummies Questions & Answers 1 10-31-2008 01:48 AM
Awk Conditional icy_blu_blu Shell Programming and Scripting 3 07-18-2008 02:34 AM
Conditional FTP Dastard Shell Programming and Scripting 2 06-21-2008 08:38 AM
conditional ftp dr46014 UNIX for Dummies Questions & Answers 3 06-20-2008 12:00 PM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 01:24 PM

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

Join Date: Mar 2008
Posts: 12
If conditional

Hi,

I am new to unix and shell scripting.In my script,there is a line using the "if" conditional -

if [$x != "," ] && [$x != ","]; then
do something

Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while running this. The error says - " != Expecting unary operator" . Please let me know what is wrong and how to correct it.
  #2 (permalink)  
Old 10-31-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
You should protect your variables with quotes, and also you need spaces around the [ and ] characters, e.g.


Code:
if [ "$x" != "a_string" ] && [ "$x" != "," ]; then
    #do something
fi

  #3 (permalink)  
Old 10-31-2008
abhinavsinha abhinavsinha is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
Please help me.
  #4 (permalink)  
Old 10-31-2008
abhinavsinha abhinavsinha is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
Thanks a lot Annihilanic. It is working now.
  #5 (permalink)  
Old 10-31-2008
abhinavsinha abhinavsinha is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
There is one more question. I have a string(suppose $str). If it starts with a comma(there can be more than one comma at the start),I have to remove all the commas from the beginning.So i have to check if the string starts with a comma.If it does,I have to delete all the commas which are at the start of the string.Please help.
  #6 (permalink)  
Old 10-31-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311

Code:
root@isau02:/data/tmp/testfeld> echo ",,,weeeeeekend"| sed 's/^,\+//'
weeeeeekend


Last edited by zaxxon; 10-31-2008 at 03:41 AM..
  #7 (permalink)  
Old 10-31-2008
abhinavsinha abhinavsinha is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
Thanks for your reply.But what I need is,I want to delete starting commas from the variable (say $str) and the changes should be made to the original variable so that when I access $str the next time,the starting commas are not there.Please help.
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 09:22 PM.


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