Logo Background

Shell Script Check File Extension Plus Format

  • By on July 30, 2010 | No Comments

    Is there any shell script that we can use to check the file extension, format or even the readability? The answer is yes and we recommend you plug in these code into program before processing any files.

    Why check the file extension, format and the readability important in your shell script program?

    It’s vital to confirm that the filename your system receive for processing is in .txt or .dat as agreed with other source system.

    The second thing you want to make sure is the file format in ascii instead of binary before loading. In the end, confirm the readability of the flat file using the -r feature in shell command.

    Refer the shell script as below to check the flat file extension, format and the readability.

    #!/bin/bash
     
    v_file_name=oracle.txt
    v_ext=`echo $v_file_name | cut -f2 -d '.'`
     
    if [ "$v_ext" != "txt" ]
    then
        echo "File is not .txt extension"
        exit 1
    else
        echo "File is with .txt extension"
    fi
     
    v_read=`file $v_file_name | cut -f2`
    if [ "$v_read" != "ascii text" ]
    then
        echo "File is not in ascii text format"
        exit 1
    else
        echo "File is in ascii text format"
    fi
     
    if [ ! -r $v_file_name ]
    then
        echo "File is not readable"
        exit 1
    else
        echo "File is readable"
    fi
    Previous
    Next
    » Oracle Session Longops - Long Running SQL
Leave a Comment