linuxbashcsvunixmd5sum

Is there a way to pass multiple values into a CSV file, based on the output of a linux script


I have written a small script that will take the users input and then generate the md5sum values for it

count = 0
echo "Enter number of records"
read number
while [ $count -le $number ]
do
    echo "Enter path"
    read path
    echo "file name"
    read file_name
    md5sum $path"/"$filename  #it shows the md5sum value and path+filename
    ((count++))
done

How can I pass these values ( path,file name, and md5sums ) to CSV file. ( assuming the user chooses to enter more than 1 record)

The output should be like

/c/training,sample.txt,34234435345346549862123454651324      #placeholder values
/c/file,text.sh,4534534534534534345345435342342

Solution

  • Interactively prompting for the number of files to process is just obnoxious. Change the script so it accepts the files you want to process as command-line arguments.

    #!/bin/sh
    md5sum "$@" |
    sed 's%^\([0-9a-f]*\)  \(\(.*\)/\)?\([^/]*\)$%\3,\4,\1%'
    

    There are no Bash-only constructs here, so I switched the shebang to /bin/sh; obviously, you are still free to use Bash if you like.

    There is a reason md5sum prints the checksum before the path name. The reordered output will be ambiguous if you have file names which contain commas (or newlines, for that matter). Using CSV format is actually probably something you should avoid if you can; Unix tools generally work better with simpler formats like tab-delimited (which of course also breaks if you have file names with tabs in them).