Skip to content
  • Stuff
  • Travel
  • Beverages
  • Support Antipaucity
  • Projects
  • About

antipaucity

fighting the lack of good ideas

shell scripting

Posted on 15 October 200915 October 2009 By antipaucity 5 Comments on shell scripting

I’ve been playing around with bash scripting quite a bit recently in relation to my current job.

Came up with one that’s really useful (imho) around chkconfig:

# mass set all services known to chkconfig to be `on` or `off` at given level
# written by warren myers - warren@warrenmyers.com
# 28 sep 2009

echo "USAGE:"
echo " $0 <level> [on|off]"
echo

# list all services, just the name, skip blank lines, do in order
SERVICES=`chkconfig --list | cut -f 1 | grep -v ^$ | grep -v ':' | sort`

for SERVICE in $SERVICES
do
  chkconfig --level $1 $SERVICE $2
  echo "$SERVICE has been altered for $1 to state $2"
done

Yes – there’s an evil you could perform:

for CS in `chkconfig --list | cut -f 1 | grep -v ^$ | grep -v ':'`
do
  chkconfig --level 12345 $CS off
done

So, if you wanted to stop all services from coming on at startup, you could – and not know you did it until you rebooted.

code

Post navigation

Previous Post: chips
Next Post: end6 must die

More Related Articles

tiny code code
gold medallion code
wordpress plugins code
the deadly sins of programming – again? code
what is the “new” python? code
firsts – programming code

Comments (5) on “shell scripting”

  1. Alexei says:
    15 October 2009 at 09:10

    Warren,

    just a style/good-practice recommendation:

    Instead of:
    SERVICES=`chkconfig –list | cut -f 1 | grep -v ^$ | grep -v ‘:’ | sort`

    Use:
    SERVICES=$(chkconfig –list | cut -f 1 | grep -v ^$ | grep -v ‘:’ | sort)

    It’s easier to read than the ticks. 😉

  2. Alexei says:
    15 October 2009 at 09:52

    Also, you might want to turn the usage message into a function. I do that all the time:

    ———- cut here ———–
    usage() {
    echo “usage: ${0##*/} [on|off]” >&2
    exit 1
    }

    # and then, some parameter checking
    [[ -z “$1” || -z “$2” ]] && usage
    [[ “$2” == “on” || “$2” == “off” ]] || usage
    ———- cut here ———–

  3. antipaucity says:
    15 October 2009 at 11:51

    Thanks, @Alexei – I do typically do a usage of some kind, and of course checking your input is *always* a good idea =D

    Curious, though – why is the $() preferable to “ for grabbing the output of an exec’d command into the variable?

  4. Alexei says:
    16 October 2009 at 11:28

    Legibility. I work with UNIX for about 15 yrs now, shell-scripting probably the same. I *know* the ticks well enough. But every now and then, I still get caught in a messy sequence of ‘ and ” and `, mostly when they’re nested.

    Thus, replacing the backtick with $( ) makes things one degree less messy.

    IMHO, naturally 😉

  5. antipaucity says:
    17 October 2009 at 03:46

    Fair enough – rarely if ever see the $() construct in the environments I’ve been exposed-to: but good to know about 🙂

Comments are closed.

October 2009
S M T W T F S
 123
45678910
11121314151617
18192021222324
25262728293031
« Sep   Nov »
RSS Error: WP HTTP Error: cURL error 60: SSL: no alternative certificate subject name matches target hostname 'paragraph.cf'

Books

  • Debugging and Supporting Software Systems
  • Storage Series

External

  • Backblaze
  • Cirkul
  • Fundrise
  • Great Big Purple Sign
  • Password Generator
  • PayPal
  • Tech News Channel on Telegram
  • Vultr
  • Wish List

Other Blogs

  • Abiding in Hesed
  • Chris Agocs
  • Eric Hydrick
  • Jay Loden
  • Paragraph
  • skh:tec
  • Tech News Channel on Telegram
  • Veritas Equitas

Profiles

  • LinkedIn
  • Server Fault
  • Stack Overflow
  • Super User
  • Telegram
  • Twitter

Resume

  • LinkedIn
  • Resume (PDF)

Services

  • Datente
  • IP check
  • Password Generator
  • Tech News Channel on Telegram

Support

  • Backblaze
  • Built Bar
  • Cirkul
  • Donations
  • Fundrise
  • PayPal
  • Robinhood
  • Vultr
  • Wish List

35-questions 48laws adoption automation blog blogging books business career centos cloud community documentation email encryption facebook google history how-to hpsa ifttt linux money networking politics prediction proxy review scifi security social social-media splunk ssl startup storage sun-tzu tutorial twitter virtualization vmware wordpress work writing zombie

Copyright © 2025 antipaucity.

Powered by PressBook Green WordPress theme