Shell Script: Shell Script Command Line Arguments

Obviously you do not want to add your Username/Password to scripts so how can you enter those and make use of execute_cmd and sftp_file without them?  Simple provide them as command line arguments in your master script that organizes all the steps.

#!/bin/bash
usage() {
        echo "Usage: $0 [-u <username>] [-p <passwrd>]" 1>&2
        echo "-u Intranet Password" 1>&2
        echo "-p Intranet Password" 1>&2
        exit 1
 }
 
while getopts "u:p:" o; do
    case "${o}" in
        u)
            username=${OPTARG}
            ;;
        p)
            password=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
 
shift $((OPTIND-1))
 
if [ "$samepwd" = true ] ; then
        ftppassword=${password}
        ftpid=${username}
fi
 
if [ -z "${username}" ] || [ -z "${password}" ]; then
    usage
fi
 
echo Username: ${username}
echo Passsword: ${password}

Usage:  command_name  -u <username> -p <password>

Now you can extend this out, and when I get a chance I’ll do another post on how to add additional arguments to this script.

  • Ghostprogrammer
  • I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: