IT3110 - System Automation

IT3110 @ utahtech

Practice 3

If you practice something, it will become easier to do!

Sample 1

In the example below, you can practice receiving command-line input. Don’t forget to chmod +x foo.sh (assuming you have named the script foo.sh).

   #!/bin/bash
   
   echo "This demonstrates comand line input"
   echo "Usage: ./sample3 foo bar hello"
   
   echo "The value of zero arg is $0"
   echo "The value of first arg is $1"
   echo "The value of second is $2"
   echo "The value of third is $3"
   echo "The value of all is $*"
   
   #hmmm, do you have to pass in a fourth value here?       
   four=${4:-carlos}
   echo "The value of fourth is $four"
  
   #how to set a variable 
   value="nacho libre"
   echo "The value is ${value}"
  
   #this line demonstrates how to ask the user for a value
   # the results in stored in the variable `age` 
   read -p "How old are you?" age
   echo "Your age is ${age}"

In the above example, here are some items that you should pay attention to:

Sample 2

The following is a very simple script that executes a command over ssh:

    #!/bin/bash
    
    echo "This is my first script"; ssh jfrancom@vm.cs.utahtech.edu "/qemu/bin/citv showvm | grep 3110"; exit 0

Don’t forget to make it executable. Note that you would substitute your username instead of jfrancom. To make it run smoother, it would be nice to copy your ssh keys to the remote machine (named vm). This would ONLY display something if you had a virtual machine created for your 3110 course.

Things to pay attention to:

See if you can write a simple script (or modify the one above) to ssh to a remote host and execute a command. Perhaps it’s as simple as ls (of course you would use the full path /usr/bin/ls)

Run the sample and redirect the standard ouput to a file.