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
Setting Variable to oldest file in a directory spaceherpe61 Shell Programming and Scripting 5 12-26-2008 01:18 PM
Piping to a file and setting filename using a variable mandriver Shell Programming and Scripting 8 09-16-2008 01:53 PM
sed - searching for string and storing in variable melias Shell Programming and Scripting 4 04-12-2008 03:57 PM
Searching for a string variable mattrix UNIX for Dummies Questions & Answers 2 03-04-2008 04:04 PM
setting file count to a variable ecupirate1998 Shell Programming and Scripting 3 07-19-2005 10:41 AM

Reply
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 08-25-2009
garth6@hotmail. garth6@hotmail. is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
setting a variable by searching within a file

Hi,

I am trying to set a variable to be used in later scripting, and am having some difficulty. I want to look in a file called scan.info and find the line that says "variable ok". Then I want to cut the number at the beginning of that line and assign that number as a variable so that later commands can refer back to ${variable}. I've tried both:

variable = $( grep "variable ok" scan.info | cut -c2-3 )
set variable = $( grep "variable ok" scan.info | cut -c2-3 )

I know that the part in parenthesis works when I type it into the terminal, but when I try to run it in a script with the set variable aspect added, it says:

Illegal variable name.

The part I'm most confused about is that the same command line works in a script used by a coworker, but not in my script.

Any ideas? Thanks!
  #2 (permalink)  
Old 08-25-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Doing yours I got a 0 as the variable using the below file.

Here is how to do it:

Code:
me@hostname:~/tmp> cat tmp.sh
#!/bin/sh

var=$(grep "variable ok" scan.info | cut -d' ' -f 1)
echo $var
me@hostname:~/tmp> cat scan.info
10 variable ok
20 testing
30 test
me@hostname:~/tmp>
This way is assuming that the number you want is always at the beginning and there is always going to be a space right after it.
  #3 (permalink)  
Old 08-25-2009
garth6@hotmail. garth6@hotmail. is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
I tried that way, and it still didn't work. I'm using csh, could that be why?

My scan.info file is set up like:

1 test ok 512 512 3
2 testing ok 64 64 1
3 variable error 56 64 1
4 variable ok 56 64 1
etc

So I want to set alpha to the number 4. I tried:

alpha = $(grep "variable ok" scan.info | cut -d" " -f 1) but still got the Illegal variable name error.

Any idea?
  #4 (permalink)  
Old 08-25-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Quote:
Originally Posted by garth6@hotmail. View Post
I tried that way, and it still didn't work. I'm using csh, could that be why?

My scan.info file is set up like:

1 test ok 512 512 3
2 testing ok 64 64 1
3 variable error 56 64 1
4 variable ok 56 64 1
etc

So I want to set alpha to the number 4. I tried:

alpha = $(grep "variable ok" scan.info | cut -d" " -f 1) but still got the Illegal variable name error.

Any idea?
I'm not very familiar with the c shell, but this seemed to work:

Code:
#!/bin/csh

set var=`grep "variable ok" scan.info | cut -d' ' -f 1`
echo $var
  #5 (permalink)  
Old 08-25-2009
garth6@hotmail. garth6@hotmail. is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
Thanks! I got it to work!
Reply

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:04 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