Revised media ingest script












0












$begingroup$


link to original question: Simple media ingest script



As per answers I have refactored my code and would like to get feedback on the current structure.



The purpose of this tool is to allow someone to copy media from cards (SD cards, CFast, etc) using rsync and to also allow someone to complete incomplete transfer that they have initiated.



I've chosen to use prompts because it's more user friendly for the environment the script will be used in. The intended users are not command line tool friendly.



I'm not very familiar with bash and am quite out of practice with code in general so I would like to check behind myself. I received excellent feedback in the previous post and have addressed several of the points raised.



#!/usr/local/bin/bash
set -e
set -u

# prompt user for input
prompt_user(){
while [[ -z "${!2:-}" ]]
do
read -r -p "$1" "$2"
done
}

# prompt user for additional rsync options
add_rsync_options(){
while true;
do
read -r -p "Add rsync options? [y/n] " add_options
case $add_options in
[Yy]* )
read -r -p $'Enter additional rsync options:n' rsync_options
break;;
[Nn]* )
break;;
*) echo "Please enter y or no!"
esac
done
}

# make target directory for transfer
make_directory(){
echo $'Follow the prompt to create a project directory.n'

prompt_user $'Path of target directory?n' target_directory
prompt_user $'Brand Prefix?n' brand_prefix
prompt_user $'Project Name?n' project_name
prompt_user $'Media Type?n' media_type
prompt_user $'Location?n' location
prompt_user $'Employee?n' employee

destination_path=${target_directory}/$(date +'%Y%m%d')_${brand_prefix}_${project_name}_${media_type}_${location}_${employee}

echo "Creating directory: ${destination_path}"

mkdir -p "${destination_path}" "${destination_path}/logs"
}

# run rsync command
run_rsync(){
echo $'Follow the prompt to complete the rsync command.n'

prompt_user $'Path to source media?n' source_path

# if partial ingest indicate pre-existing target directory
if [[ "$option" == "2" ]]; then
prompt_user $'Target directory?n' target_directory
destination_path=$target_directory
fi

add_rsync_options

echo $'Running rsync command:n'

rsync -r --info=progress2 --log-file="${destination_path}/logs/$(date +'%Y%m%d')_transfer_log.txt" ${rsync_options:-} "${source_path}/" "${destination_path}"

}

# clear terminal for readability
clear

# start
while true
do
# read user input and run appropriate functions
read -r -p $'Enter [1] to start an ingest or [2] to complete a partial ingest.n' option
case $option in
1 )
make_directory
run_rsync
break;;
2 )
run_rsync
break;;
* )
echo $'Please enter a valid option!n';;
esac
done


Error handling is at a minimum still I believe and I'm not too sure what would even be relevant to handle past what's implemented. I'm not very experienced in that regard so resources are appreciated!



Thanks!










share|improve this question







New contributor




usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    link to original question: Simple media ingest script



    As per answers I have refactored my code and would like to get feedback on the current structure.



    The purpose of this tool is to allow someone to copy media from cards (SD cards, CFast, etc) using rsync and to also allow someone to complete incomplete transfer that they have initiated.



    I've chosen to use prompts because it's more user friendly for the environment the script will be used in. The intended users are not command line tool friendly.



    I'm not very familiar with bash and am quite out of practice with code in general so I would like to check behind myself. I received excellent feedback in the previous post and have addressed several of the points raised.



    #!/usr/local/bin/bash
    set -e
    set -u

    # prompt user for input
    prompt_user(){
    while [[ -z "${!2:-}" ]]
    do
    read -r -p "$1" "$2"
    done
    }

    # prompt user for additional rsync options
    add_rsync_options(){
    while true;
    do
    read -r -p "Add rsync options? [y/n] " add_options
    case $add_options in
    [Yy]* )
    read -r -p $'Enter additional rsync options:n' rsync_options
    break;;
    [Nn]* )
    break;;
    *) echo "Please enter y or no!"
    esac
    done
    }

    # make target directory for transfer
    make_directory(){
    echo $'Follow the prompt to create a project directory.n'

    prompt_user $'Path of target directory?n' target_directory
    prompt_user $'Brand Prefix?n' brand_prefix
    prompt_user $'Project Name?n' project_name
    prompt_user $'Media Type?n' media_type
    prompt_user $'Location?n' location
    prompt_user $'Employee?n' employee

    destination_path=${target_directory}/$(date +'%Y%m%d')_${brand_prefix}_${project_name}_${media_type}_${location}_${employee}

    echo "Creating directory: ${destination_path}"

    mkdir -p "${destination_path}" "${destination_path}/logs"
    }

    # run rsync command
    run_rsync(){
    echo $'Follow the prompt to complete the rsync command.n'

    prompt_user $'Path to source media?n' source_path

    # if partial ingest indicate pre-existing target directory
    if [[ "$option" == "2" ]]; then
    prompt_user $'Target directory?n' target_directory
    destination_path=$target_directory
    fi

    add_rsync_options

    echo $'Running rsync command:n'

    rsync -r --info=progress2 --log-file="${destination_path}/logs/$(date +'%Y%m%d')_transfer_log.txt" ${rsync_options:-} "${source_path}/" "${destination_path}"

    }

    # clear terminal for readability
    clear

    # start
    while true
    do
    # read user input and run appropriate functions
    read -r -p $'Enter [1] to start an ingest or [2] to complete a partial ingest.n' option
    case $option in
    1 )
    make_directory
    run_rsync
    break;;
    2 )
    run_rsync
    break;;
    * )
    echo $'Please enter a valid option!n';;
    esac
    done


    Error handling is at a minimum still I believe and I'm not too sure what would even be relevant to handle past what's implemented. I'm not very experienced in that regard so resources are appreciated!



    Thanks!










    share|improve this question







    New contributor




    usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      link to original question: Simple media ingest script



      As per answers I have refactored my code and would like to get feedback on the current structure.



      The purpose of this tool is to allow someone to copy media from cards (SD cards, CFast, etc) using rsync and to also allow someone to complete incomplete transfer that they have initiated.



      I've chosen to use prompts because it's more user friendly for the environment the script will be used in. The intended users are not command line tool friendly.



      I'm not very familiar with bash and am quite out of practice with code in general so I would like to check behind myself. I received excellent feedback in the previous post and have addressed several of the points raised.



      #!/usr/local/bin/bash
      set -e
      set -u

      # prompt user for input
      prompt_user(){
      while [[ -z "${!2:-}" ]]
      do
      read -r -p "$1" "$2"
      done
      }

      # prompt user for additional rsync options
      add_rsync_options(){
      while true;
      do
      read -r -p "Add rsync options? [y/n] " add_options
      case $add_options in
      [Yy]* )
      read -r -p $'Enter additional rsync options:n' rsync_options
      break;;
      [Nn]* )
      break;;
      *) echo "Please enter y or no!"
      esac
      done
      }

      # make target directory for transfer
      make_directory(){
      echo $'Follow the prompt to create a project directory.n'

      prompt_user $'Path of target directory?n' target_directory
      prompt_user $'Brand Prefix?n' brand_prefix
      prompt_user $'Project Name?n' project_name
      prompt_user $'Media Type?n' media_type
      prompt_user $'Location?n' location
      prompt_user $'Employee?n' employee

      destination_path=${target_directory}/$(date +'%Y%m%d')_${brand_prefix}_${project_name}_${media_type}_${location}_${employee}

      echo "Creating directory: ${destination_path}"

      mkdir -p "${destination_path}" "${destination_path}/logs"
      }

      # run rsync command
      run_rsync(){
      echo $'Follow the prompt to complete the rsync command.n'

      prompt_user $'Path to source media?n' source_path

      # if partial ingest indicate pre-existing target directory
      if [[ "$option" == "2" ]]; then
      prompt_user $'Target directory?n' target_directory
      destination_path=$target_directory
      fi

      add_rsync_options

      echo $'Running rsync command:n'

      rsync -r --info=progress2 --log-file="${destination_path}/logs/$(date +'%Y%m%d')_transfer_log.txt" ${rsync_options:-} "${source_path}/" "${destination_path}"

      }

      # clear terminal for readability
      clear

      # start
      while true
      do
      # read user input and run appropriate functions
      read -r -p $'Enter [1] to start an ingest or [2] to complete a partial ingest.n' option
      case $option in
      1 )
      make_directory
      run_rsync
      break;;
      2 )
      run_rsync
      break;;
      * )
      echo $'Please enter a valid option!n';;
      esac
      done


      Error handling is at a minimum still I believe and I'm not too sure what would even be relevant to handle past what's implemented. I'm not very experienced in that regard so resources are appreciated!



      Thanks!










      share|improve this question







      New contributor




      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      link to original question: Simple media ingest script



      As per answers I have refactored my code and would like to get feedback on the current structure.



      The purpose of this tool is to allow someone to copy media from cards (SD cards, CFast, etc) using rsync and to also allow someone to complete incomplete transfer that they have initiated.



      I've chosen to use prompts because it's more user friendly for the environment the script will be used in. The intended users are not command line tool friendly.



      I'm not very familiar with bash and am quite out of practice with code in general so I would like to check behind myself. I received excellent feedback in the previous post and have addressed several of the points raised.



      #!/usr/local/bin/bash
      set -e
      set -u

      # prompt user for input
      prompt_user(){
      while [[ -z "${!2:-}" ]]
      do
      read -r -p "$1" "$2"
      done
      }

      # prompt user for additional rsync options
      add_rsync_options(){
      while true;
      do
      read -r -p "Add rsync options? [y/n] " add_options
      case $add_options in
      [Yy]* )
      read -r -p $'Enter additional rsync options:n' rsync_options
      break;;
      [Nn]* )
      break;;
      *) echo "Please enter y or no!"
      esac
      done
      }

      # make target directory for transfer
      make_directory(){
      echo $'Follow the prompt to create a project directory.n'

      prompt_user $'Path of target directory?n' target_directory
      prompt_user $'Brand Prefix?n' brand_prefix
      prompt_user $'Project Name?n' project_name
      prompt_user $'Media Type?n' media_type
      prompt_user $'Location?n' location
      prompt_user $'Employee?n' employee

      destination_path=${target_directory}/$(date +'%Y%m%d')_${brand_prefix}_${project_name}_${media_type}_${location}_${employee}

      echo "Creating directory: ${destination_path}"

      mkdir -p "${destination_path}" "${destination_path}/logs"
      }

      # run rsync command
      run_rsync(){
      echo $'Follow the prompt to complete the rsync command.n'

      prompt_user $'Path to source media?n' source_path

      # if partial ingest indicate pre-existing target directory
      if [[ "$option" == "2" ]]; then
      prompt_user $'Target directory?n' target_directory
      destination_path=$target_directory
      fi

      add_rsync_options

      echo $'Running rsync command:n'

      rsync -r --info=progress2 --log-file="${destination_path}/logs/$(date +'%Y%m%d')_transfer_log.txt" ${rsync_options:-} "${source_path}/" "${destination_path}"

      }

      # clear terminal for readability
      clear

      # start
      while true
      do
      # read user input and run appropriate functions
      read -r -p $'Enter [1] to start an ingest or [2] to complete a partial ingest.n' option
      case $option in
      1 )
      make_directory
      run_rsync
      break;;
      2 )
      run_rsync
      break;;
      * )
      echo $'Please enter a valid option!n';;
      esac
      done


      Error handling is at a minimum still I believe and I'm not too sure what would even be relevant to handle past what's implemented. I'm not very experienced in that regard so resources are appreciated!



      Thanks!







      bash






      share|improve this question







      New contributor




      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 hours ago









      usulmuaddibusulmuaddib

      163




      163




      New contributor




      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      usulmuaddib is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          usulmuaddib is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216286%2frevised-media-ingest-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          usulmuaddib is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          usulmuaddib is a new contributor. Be nice, and check out our Code of Conduct.













          usulmuaddib is a new contributor. Be nice, and check out our Code of Conduct.












          usulmuaddib is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216286%2frevised-media-ingest-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?