Re: Sed in Script problem using expr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14Nov2006 11:14, Paul Ward <pnward@xxxxxxxxxxxxxx> 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@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

Trust the computer... the computer is your friend.
        - Richard Dominelli <dominel@xxxxxxxxx>

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux