The code below creates a file and accepts an input argument through the command line.
I want to do two things:
If the user forgot to enter the input on the command line, the system should give some sort of alert or message. Assume if I forgot to give an input argument, then the system should not proceed with the script execution.
Assume if the system tries to create the already existing file, at present we are managing with showing a message like "File already exists", but instead I want to ask something like "File already exists, are you sure you want to override? yes/no". If he answers yes, then simply override the existing one, else the system should ask for another input from the user.
#!/usr/local/bin/perl
#print "content-type: text/html \n\n"; #HTTP HEADER
$numArgs = $#ARGV + 1;
foreach $argnum (0 .. $#ARGV) {
$GET_ALL_USER_INPUTS = "$ARGV[$argnum]\n";
}
@INPUT_ARR = split(/,/, $GET_ALL_USER_INPUTS);
$filename = "DD_WRITE_${INPUT_ARR[0]}.txt";
$GET_ARR_SIZE = scalar @INPUT_ARR;
$CLIENT_NAME = "T-sys";
$DD_CONTENT = "Design Document ${INPUT_ARR[0]} - ${CLIENT_NAME} :-\n";
$DD_CONTENT .= "--------------------------------------";
#get the no length and generate dotted lines
for($i=0;$i<=length(${INPUT_ARR[0]});$i++){
$DD_CONTENT .= "-";
}
$DD_CONTENT .= "--------------\n";
$DD_CONTENT .= "Database Details\n";
if (-e "${filename}") {
print "File exists!";
exit;
}
else {
open(FILE, ">", "$filename") or die "Cannot open $filename - $!";
print FILE "${DD_CONTENT}\n";
close (FILE);
}