Pass command line arguments to bash script

  • I am new to bash script programming.

    I want to implement a bash script 'deploymLog', which accepts as input one string argument(name).

    [[email protected] Desktop]# ./deploymLog.sh name
    

    here I want to pass the string argument(name) through command line

    As an initial step, I need to append the current timestamp along with this input string to a log file say Logone.txt in current directory in the below format:

    [name]=[System time timestamp1]
    

    How it is possible?

  • $> cat ./deploymLog.sh 
    #!/bin/bash
    
    name=$1
    log_file="Logone.txt"
    
    if [[ -n "$name" ]]; then
        echo "$1=$( date +%s )" >> ${log_file}
    else
        echo "argument error"
    fi
    

    The first argument from a command line can be found with the positional parameter $1. [[ -n "$name" ]] tests to see if $name is not empty. date +%s returns the current timestamp in Unix time. The >> operator is used to write to a file by appending to the existing data in the file.

    $> ./deploymLog.sh tt
    
    $> cat Logone.txt 
    tt=1329810941
    
    $> ./deploymLog.sh rr
    
    $> cat Logone.txt 
    tt=1329810941
    rr=1329810953
    

    For more readable timestamp you could play with date arguments.

    total beginner here... it would be helpful to know what the `[[ -n "$name" ]]` part is doing.

    Yup, that^s true I'm also a total noob and my script is dying on that line???

    The "[[ -n "$name" ]]" is another form of the "test" command. See: http://ss64.com/bash/test.html

  • Shell command line arguments are accessible via $1 (the first), $n (the nth), or $* (all arguments), so your script should start:

    #!/bin/bash
    
    if [ $# -ne 1 ]; then
        echo $0: usage: myscript name
        exit 1
    fi
    
    name=$1
    

    Now the name argument is accessible from the script as $name.

    To get the timestamp use the date(1) command and give it a format specifier so it produces the format you want:

    now=$(date +%Y%m%d%H%M%S)
    

    Now $now contains the current date and time.

    So you can create your log file thus:

    logfile=/path/to/log/file/mylogfile.$now
    echo "[$name]=[$now]" >> $logfile
    

    You are better off using a shell function to log your messages as it will be easier to use:

    function logit
    {
        now=$(date +%Y%m%d%H%M%S)
        echo "$now: $*" >> $logfile
    }
    

    Note that shell functions access their own arguments in the same way as the script (via $1 etc.)

    So the initial script looks like this:

    #!/bin/bash
    
    function logit
    {
        now=$(date +%Y%m%d%H%M%S)
        echo "$now: $*" >> $logfile
    }
    
    if [ $# -ne 1 ]; then
        echo $0: usage: myscript name
        exit 1
    fi
    
    name=$1
    now=$(date +%Y%m%d%H%M%S)
    logfile=/path/to/log/file/mylogfile.$now
    
    logit name = $name
    

    (note the log file isn't in the exact format you specified; it's in a better one with the timestamp at the start of each line).

  • #!/bin/bash
    
    name=$1
    
    echo "$(date '+%Y%m%d-%H:%M:%S') => " $name >> x.log
    

    run "bash deploymLog.sh whatever", and you got x.log with

    20120220-23:53:50 =>  whatever
    

    when you vote down, give a reason, thanks.

    I suppose the downvoter missed the reason of `name=$1`. The variable $name is never used, as you output only the string 'name' literally.

    oh, my bad. thanks for pointing out. i need to be more careful.

    @DynoHongjunFu Still, the variable has same name and value, which is not a good way to make the example readable.

    It is best to quote the var when assigning to name like this: `name="$1"`

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM