Miscellaneous interview questions on UNIX scripting.

In this I have given all miscellaneous interview questions related to UNIX. For Unix interview questions we should try to give one line commands as much as possible. So for all the below questions I have tried give very simpler answers.

 

1). How can we check if a csv file has same number columns in each row.

When we work on Bigdata applications, most of the we will get files from client which might have corrupted data. We can't guarantee that each row will have data according to the schema. So now our requirement is filter the data that has a specific number columns in each row. Below is the command that filters data with particular number of columns in each row and in this let's assume it to be 3 columns. We need to filter all the rows have exactly three columns from a file having name sample.csv.

Below is the UNIX command that perform this operation:

awk -F '|' 'NF==3' sample.csv

-F  -> This is the argument to specify the delimiter used in the file. We can change to any other valid delimiter. (For ex: awk -F ',')

 

2). We have two UNIX scripts, script1 and script 2. We are calling script1 inside script2. Now we want to continue the execution of script2 if and only if the script1 execution is successful. How can we achieve this?

sh script1.sh
if [ $? -eq 0 ]
then
     echo "Script1 executed successfully, continuing further"
     #Commands to proceed futher
else
    echo "Script1 failed to execute. Exiting script2"
    exit 1;
fi

Here $? is the key role player. $? stores the execution status of previous command, which means it stores the exit status of previous command. In UNIX environment if a script executes successfully then exit code 0. The same script can be used to check if the execution of previous command is successful and proceed further.

 

Leave a Reply

Your email address will not be published. Required fields are marked *