Changes

Jump to: navigation, search

OPS435 Lab 2 - Bash

1,888 bytes added, 00:39, 22 January 2016
Created page with '= Your job = Get the following script working and submit it via Blackboard: <source lang="bash"> #!/bin/bash # OPS435 # Lab 2 2016-01 script # Fix this script so that: # - it d…'
= Your job =

Get the following script working and submit it via Blackboard:

<source lang="bash">
#!/bin/bash
# OPS435
# Lab 2 2016-01 script
# Fix this script so that:
# - it doesn't produce any errors on matrix
# - it does tell you your IP address
#

PATH=$PATH:

# The device we care about
IFNAME=eth0

# Save the output from ifconfig into the IFCFG variable
IFCFG=`ifconfig $IFNAME`

# Find the IP address and store it in the ADDR variable
ADDR=`echo "IFCFG" | grep 'inet addr' | awk '{print ...;}'`

# Make sure it doesn't contain "addr:"
IPONLY=`echo $ADDR | cut -d... -f ... `

# Print the IP address
echo IPONLY
</source>

= Notes =

Here are some tricks to help you debug problems in shell scripts:

* Unless you already know the entire script is working and there's only one error (which is not the case here): fix it one line at a time. That means:
** Insert an '''exit''' command right after the line you're testing, that way you're not seing errors you're not yet working on.
** Insert a temporary '''echo''' statement before the exit to test the values of variables to make sure they are what you expect them to be.
* If the '''which''' command doesn't tell you where a program is - try the '''whereis''' command. which looks in your path only, whereis searches more places.
* Modifying the '''PATH''' in a shell script doesn't change the PATH variable in your entire session, but only inside the script. The .bashrc you've edited in the last lab is an exception, it gets executed automatically with each shell you open.
* '''Complex statements''' that don't work (e.g. the ADDR= line in the script above) can be broken into parts. Start with the first component and fix it, then add the second part, fix that, etc.
* Try to figure out the problem yourself but if it's taking too long - by all means '''ask your professor''' for help explaining things you don't understand.

Navigation menu