Posts

Showing posts with the label bash

Command Line Options: How To Parse In Bash Using “getopt”

Image
Use “getopt” in a Bash script to parse long and short command line options, enforce arguments for some options and spot dubious user input. Most of the times, when you need to automate system administration tasks on your Linux machine, writing a Bash script is your best bet. And sometimes, you need to be able to control the behaviour of your script at some point which leaves you with two choices: use environment variables (set before running the script) as sort of a flag or the better and more intuitive way to use command line arguments. What is "getopt"? getopt is a program that parses command line options in shell scripts. It is the enhanced version of older getopts and uses the getopt C library to do its job. It is compatible with getopts as long as GETOPT_COMPATIBLE environment variable is set, however some of it best features are not available in compatibility mode. An overview of command options Command line input is viewed in 3 categories by getopt: short ...

How To Batch-set Environment Variables

Learn how to automatically (dump) set environment variables defined in a file. Sometimes you have a file (usually called .env or .environment ) with dozen of environment variables defined and you have to set those variables before running a command --for example this happens in Django projects with a .env file for settings. The solution is easy: export $(cat ENVIRONMENT_FILE | xargs) && YOUR_COMMAND