shell variables and sed substitution


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users shell variables and sed substitution
# 1  
Old 05-02-2008
shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION)
value in a kernel Makefile.


So, I have a shell script that takes one parameter, a version string:

buildkernel.sh 2.6.18.21.7-custom

In the script, I assign the parameter to K_VER:

K_VER="2.6.18.21.7-custom"

I perform string operations until I get this:
XTRA_K_VER=".21.7-custom"

I export it (thinking that the proper value would be substitued when
make was run, but the variable is not expanded):
export XTRA_K_VER

So, maybe I should forget the export?

I am using the variable, XTRA_K_VER, in a substitution in a
Makefile with the idea that the variable would be expanded:

sed -f script.sed <Makefile.old >Makefile.new

where script.sed contains the following:

s#\([[:blank:]]*EXTRAVERSION\)[[:blank:]]*=[[:blank:]]*.*#\1 = $XTRA_K_VER#

The issue I am experiencing is that I want the value of XTRA_K_VER to
be used in the Makefile as opposed to the literal variable name. I do not
have a preference whether I insert the variable form of the entity (into
the Makefile) or the literal string value of the variable.

From what I have described, is there a reasonably clean way to let the
sed script expand the value of the variable before the substitution? I
thought about re-writing the sed script dynamically upon every invocation
of the program, but that seems a bit extreme to me (and potentially
challenging for others to maintain in the future).

Thanks in advance. I really appreciate the experience of the people on this site. Smilie

Cheers,
:-D
# 2  
Old 05-02-2008
ya know.. I think i was merely missing parens around the variable
written into the Makefile. D'oh!
# 3  
Old 05-05-2008
That's right. Also just for the record, the "export" doesn't add anything; the variable is interpolated by Make itself before the command executes. (Export is for putting stuff in the environment, so you can e.g. modify the PATH of all the processes you run from your Makefile.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with substitution between two variables

To all geeks, What I want to achieve: 1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2) 2. Check if files exisits. If doesn't, then exit 3. If files exist, look for a particular string in both files 4. If the string exists, then change the... (8 Replies)
Discussion started by: Deepak Tulsani
8 Replies

2. UNIX for Dummies Questions & Answers

sed substitution and shell variables

Hello!! Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing. for ex. i've used the follow sed command.. sed "s/$var1/$var2/g" i've also tried the other... (5 Replies)
Discussion started by: a_ba
5 Replies

3. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

4. Shell Programming and Scripting

Substitution with sed

I have a file with some numbers having single quotes around them which I want to remove. i.e. '923930' -> 23930 If it can be done without using sed thats fine. I have tried with sed but can't think how to replace this pattern on only the numbers (13 Replies)
Discussion started by: user_invalid
13 Replies

5. Shell Programming and Scripting

sed command using variables in shell script

hi guys, The following command doesn't seem to work in my shell script: tag=$(sed -n '/${line}/ s/.*\.*/\1/p' myfile.txt) When i replace the ${line} with an actual value, it works fine. So, how do i use the ${line} in this sed command? Thanks in advance, Zaff (2 Replies)
Discussion started by: zaff
2 Replies

6. Shell Programming and Scripting

[bash] command line substitution with environmental variables

Hi, I'm using an array that contains compiler FLAGS that need to be executed either before ./configure or after the main 'make' command. example of array containing compiler flags. ------------------------------------------------- FLAGS="CFLAGS=\"-arch x86_64 -g -Os -pipe... (7 Replies)
Discussion started by: ASGR
7 Replies

7. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

8. Shell Programming and Scripting

SED Substitution

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as well to... (1 Reply)
Discussion started by: shubhranshu
1 Replies

9. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

10. Shell Programming and Scripting

Double Substitution variables in ksh

Hi I have a variable whose value is like this i=/test/test1/test2/myfile.cd.070505123457 i would like to have the value of myfile.cd stored into another variable my attempt is test=${i##*/} ;echo $test ##and i get myfile.cd.070505123457 since what i wnat is myfile.cd i try this... (19 Replies)
Discussion started by: xiamin
19 Replies
Login or Register to Ask a Question