Linux Imagine that you want to store all the errors generated by your script int
ID: 3824907 • Letter: L
Question
Linux
Imagine that you want to store all the errors generated by your script into an error log. Currently, you'd have to add 2>>error Log to every line that may produce an error. Instead, you can tell the script to redirect a specific input/output for the duration of the whole script using exec . For example, exec 1>test out redirects the default/normal output to a file test out , for the whole script. Write a script that takes 2 arguments: a file, and a folder. It moves that file to that folder, without te sting anything about them , or displaying any message. If errors occurred, your script will ask the user whether he/she wants to read them (Y/N) and, if so, it will display them. Use exec in this script.
Explanation / Answer
CODE:
Script usuage:<Script> <fine name> <folder name> (Example--> ksh a.ksh error.log /home/user)
#!/bin/ksh
#Two parameters should be passed to script
if [[ $# -ne 2 ]];then
echo "Please pass Two argumants"
echo "Usuage:<Script> <fine name> <folder name>"
fi
file_name=$1
folder_name=$2
full_file_path=$folder_name/$file_name
#Getting error file size before running process
file_size_before=`ls -l ${full_file_path} | awk '{$5=$5*8}1' | cut -d" " -f5`
#create error file if it doesn't exit
if [[ ! -f $full_file_path ]];then
touch
fi
#exec command
exec 2>> $full_file_path
#The below line error will be captured in the error file
ech "This line has syntax error. Instead of echo gave sch"
#Getting error file size after running process
file_size_after=`ls -l ${full_file_path} | awk '{$5=$5*8}1' | cut -d" " -f5`
#If the file size is changed, that means error is captured
if [[ ! "${full_file_path}" == "${file_size_after}" ]];then
echo "Error details are captured. Are you want to view? type Y/N"
read text
if [[ $text == "Y" ]];then
echo "----------- ERROR FILE ----------"
cat ${full_file_path}
echo "exit 0"
exit 0
else
echo "exit 0"
exit 0
fi
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.