1

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, /(\s\S*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.

2 Answers 2

1

YOu can use a while loop to read a date off the user until the date is compliant (date returns a code 0) at which point we break from the loop and execute your script (in this case myscript), passing the read date variable dat as a parameter:

while true; do read -p "Please enter a date in the format i.e. 20210201" dat; if date -d "$dat" +"%Y%m%d"; then break; fi; done myscript "$dat" 

Then in your script, amend the line:

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

to:

cDt="$1" # SEt cDt to the first passed parameter 
Sign up to request clarification or add additional context in comments.

1 Comment

I'd recommend while true; do read -p "..." dat; userDate=$(date -d "$dat" "+%Y%m%d") && break; done; do_stuff_with "$userDate" -- that way, the user can enter the date in any (valid) format, and the script can use the ISO date format for whatever it needs.
0

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 

4 Comments

this script finds the files for the current date and executes(generate ) a csv for the current date. example file_20210201<file name>, I want to generate the files for a random date example: genate the csv for the date 2021-01-27 then it should takes files on the day 27 january not the current date
possibility to pass the date suffix from outside as a parameter to script
If I understood you correctly, then the answer contains the solution to your question. The only thing I have not specified is a check that this argument exists at all. You can do it with if [[ -z "$1" ]]; then echo "no parameters"; exit; fi.
You can pass the argument without date e.g. ./myscript 20210127. To use a different date format, you can change the validation string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.