Skip to content
Advertisement

pass the date from outside as a parameter

This is my Linux script, I want to ask the user from outside what date is needed, then save that date and run the script for that date.

#/bin/bash

cDt=$(date +"%Y%m%d")

cd /home/dwh_landing/temp
echo 'Process_Date,Outfile,Outpath,Process_hour,Process_Minutes,Infile' > ccn_daily_${cDt}.csv

cd /home/dwh_landing/etl_scripts/etl_logs/

awk -F',' '{print $1 "," $2 "," $5}' *ccn-json*${cDt}* | grep 'creditControl.json' | awk -F '/' '{print $0 "," $5}' |  awk 'match($0, /(sS*k)/ , a ) {print $0 "," a[1]}' >> /home/dwh_landing/temp/ccn_daily_${cDt}.csv

cd /home/dwh_landing/temp

cat ccn_daily_${cDt}.csv | wc -l >> ccn_daily_${cDt}.csv

now currently this script generate a csv for the current date files, I want to run this for a user request date, can I pass parameters from outside? any help could be useful.

Advertisement

Answer

What do you mean by outside? Do you want to pass parameters when running the script? If so, you can run the script with

./myscript $(date +"%Y%m%d")

and use this argument with

#!/bin/bash

cDt="$1"

...

This is parameter number one, since the null parameter is the name of the script.

Also, you can validate a date string with

if ! date +"%Y%m%d" -d "$cDt" &> /dev/null; then
    echo "$1 - invalid date string"
    exit
fi
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement