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
Comparing filename-substrings and remove unnecessary files cypher82 UNIX for Dummies Questions & Answers 5 06-06-2008 05:26 AM
comparing strings agarwal Shell Programming and Scripting 3 04-16-2008 06:29 AM
Comparing Two Strings Anji Shell Programming and Scripting 8 01-09-2008 06:32 AM
Comparing strings yakyaj UNIX for Advanced & Expert Users 2 03-23-2007 01:22 AM
Breaking strings into Substrings switch Shell Programming and Scripting 4 04-06-2006 05:29 AM

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 02-19-2008
nancylt723 nancylt723 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 1
ksh: Comparing strings that contain spaces and working with substrings

Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks.

Here are two such examples.

I want to save the first 19 characters of the following string to a variable.

"Operation Completed and blah blah blah"

I know this works (from another thread):

mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19

But I cannot figure out how to save the output of the result of the cut command to a variable.

So because I could not do that, I tried to compare the result of the cut command to a variable containing "Operation Completed", only I can't get the comparison to work. I keep getting :Operation not found when I run the following shell.

cstring="Operation Completed"
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19
if [[ "$cstring" = $($mystring | cut -c 1-19) ]]; then
echo "they match"
fi

Thanks in advance.
  #2 (permalink)  
Old 02-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,764
some ways:
Code:
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19 | read myvariable
# or --------
myvariable=$(echo $mystring | cut -c 1-19 )
# or ---
myvariable=`echo $mystring | cut -c 1-19`
The ` bactick syntax is old but is allowed.
  #3 (permalink)  
Old 02-19-2008
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384
Quote:
Originally Posted by nancylt723 View Post
Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks.

Here are two such examples.

I want to save the first 19 characters of the following string to a variable.

"Operation Completed and blah blah blah"

I know this works (from another thread):

mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19

But I cannot figure out how to save the output of the result of the cut command to a variable.

So because I could not do that, I tried to compare the result of the cut command to a variable containing "Operation Completed", only I can't get the comparison to work. I keep getting :Operation not found when I run the following shell.

cstring="Operation Completed"
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19
if [[ "$cstring" = $($mystring | cut -c 1-19) ]]; then
echo "they match"
fi

Thanks in advance.
Code:
if [ "$cstring" = "$(echo $mystring | cut -c 1-19)" ]; then
  echo "they match"
fi
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 05:34 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