LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2011, 09:03 AM   #1
takayama
Member
 
Registered: Sep 2009
Posts: 97

Rep: Reputation: 0
Some simple bash questions


Ok i have some questions about bash that im sure are quite simple, the first one is about arguments.
Is it possible to assign a default value to a variable (lets say $test) and if the user types something as a second argument ($2) i want to overwrite that default value with the new one from $2.

The second one is that i want to use the ipaddress my eth0 device having as a variable.

Also i would like to have script that i can use with "multilevel" arguments, like if the script is named sysinfo, i can call it like this.

./sysinfo hdd free /dev/sda1
./sysinfo hdd free /dev/sda2
./sysinfo cpu free
etc

Last edited by takayama; 05-07-2011 at 09:10 AM.
 
Old 05-07-2011, 09:12 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
The first question is easy.
Code:
test="Default Value"
if [ "$2" ]; then
  test="$2"
fi
The second is a bit more involved. After you figure out how
to get the IP address, you can set the value like this.
Code:
ipaddress="`command1 | command2 | command3`"
To find the IP address you can use a combination of ifconfig,
grep and sed. Try to figure this out--it will be a good
challenge.
 
Old 05-07-2011, 05:27 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by takayama View Post
i want to overwrite that default value with the new one from $2.
This is basic Bash parameter expansion. Use
Code:
${2:-value}
If you have the default value in say variable default2, then use
Code:
${2:-$default2}
Quote:
Originally Posted by takayama View Post
The second one is that i want to use the ipaddress my eth0 device having as a variable.
Code:
ip0=`ifconfig eth0`
ip0="${ip0##*inet addr:}"
ip0="${ip0%% *}"
The first line runs ifconfig to get the description block for eth0. The second line removes everything before the IP address. The last line removes everything after the IP address. Therefore you end with just the IP address for eth0.

Quote:
Originally Posted by takayama View Post
Also i would like to have script that i can use with "multilevel" arguments, like if the script is named sysinfo, i can call it like this.
Use a while loop to process any command line parameters left. Use shift N to move parameters N steps left, removing the N first parameters. Here is an example skeleton:
Code:
#!/bin/bash

Usage () {
    exec >&2
    echo ""
    echo "Usage: $0 [ -h | --help ]"
    echo "       $0 hdd cmd device"
    echo "       $0 cpu cmd"
    echo ""
    exit ${1:-1}
}

# If no parameters, output usage.
[ $# -lt 1 ] && Usage 0

# Loop for as long as there are parameters left.
while [ $# -gt 0 ]; do

    # Next parameter is the "command". Remove it from the list.
    cmd="$1"
    shift 1

    # Process the command.
    case "$cmd" in

        hdd)
            # hdd cmd dev

            if [ $# -lt 1 ]; then
                echo "hdd: No command or device specified." >&2
                exit 1
            fi
            if [ $# -lt 2 ]; then
                echo "hdd: No device specified." >&2
                exit 1
            fi

            cmd="$1"
            dev="$2"
            shift 2

            # TODO: act on "hdd $cmd $dev".
            echo "HDD: cmd=$cmd, dev=$dev"
        ;;

        cpu)
            # cpu cmd

            if [ $# -lt 1 ]; then
                echo "cpu: No command specified." >&2
                exit 1
            fi

            cmd="$1"
            shift 1

            # TODO: act on "cpu $cmd".
            echo "CPU: cmd=$cmd"
        ;;

        -h|--help|help)
            Usage 0
        ;;

        *)
            echo "$1: Unknown command." >&2
            exit 1
        ;;
    esac
done
To simplify each subcommand, you could put each one in their own function. Since the positional parameters $1, $2, and so on are the parameters for the function, you'll need to save the script arguments to a separate array, and have the functions modify that array directly. In the above script, you'd have to add the array initialization, for example
Code:
arg=("" "$@") ; unset arg[0]
before anything else in the script (say, on the second line), and then do these replacements:
Code:
$1       ->  ${arg[1]}
$2       ->  ${arg[2]}
$#       ->  ${#arg[@]}
$@       ->  ${arg[@]}
shift 1  ->  unset arg[1]        ; arg=("" "${arg[@]}") ; unset arg[0]
shift 2  ->  unset arg[1] arg[2] ; arg=("" "${arg[@]}") ; unset arg[0]
Unlike positional parameters, you can then modify the script parameter list anywhere, even inside a function.

Hope this helps.

Last edited by Nominal Animal; 05-07-2011 at 05:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple questions that need simple answers Mr-Bisquit General 2 07-09-2010 02:46 PM
simple questions looking for a simple command s3ns4i Linux - Newbie 4 10-21-2008 12:55 PM
Simple Bash Questions RAdams Programming 8 08-23-2006 09:48 AM
Simple Questions...Hopfully Simple Answers. caps_phisto Linux - General 3 12-21-2004 12:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration