Sed in Script problem using expr
Cameron Simpson
cs at zip.com.au
Thu Nov 16 00:52:31 UTC 2006
On 14Nov2006 11:14, Paul Ward <pnward at googlemail.com> wrote:
| Can anyone help me with the following script problem please its driving me
| nuts.
|
| I am trying to make a script capable of commenting and uncommenting
| lines on a custom config file similar to this.
|
| myconf:
| #Months
| 02
| 04
| 06
| #08
| 10
| 12
|
| myscript.sh:
| #!/bin/bash
Say "#!/bin/sh" instead. You're not doing anything needing bash, so
your script will more portable to other platforms. Irrelevant, perhaps,
for this particular task but the habit will help you later.
| echo "Enter a number from 1 - 10 to sed"
| read i
| NUM=`expr $i - 2`; printf %02d $NUM ; echo " "
Avoid CAPTIAL names for script local variables. Because "exported"
variables like $PAGER, $HOME, $SHELL etc are generally in upper case
you run a risk of colliding with one of which you were unaware. And
since variables your script inherits are exported to subcommands, your
accidental use may quietly change the way some subcommand behaves.
Save the output of printf:
num=`expr $i - 2`
num=`printf %02d $NUM`
| echo "I will sed this " $NUM
| read
| sed '/$NUM/s/^#//' myconf > newconf
Inside single quotes parameter substituion is not performed.
Use double quotes:
sed "/$num/s/^#//' myconf > newconf
Also, running your script with the -x option will show you command line
arguments as issued to the subcommands. This is very helpful when
debugging:
sh -x your_script
Cheers,
--
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/
Trust the computer... the computer is your friend.
- Richard Dominelli <dominel at panix.com>
More information about the users
mailing list