![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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> |
|
||||
|
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? |
|
||||
|
Quote:
Code:
#!/bin/csh set var=`grep "variable ok" scan.info | cut -d' ' -f 1` echo $var |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|