Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7094143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:31:37+00:00 2026-05-28T08:31:37+00:00

I am very new to bash scripts, and for my first script attempt I

  • 0

I am very new to bash scripts, and for my first script attempt I am submitting files to my professor’s dropbox within the same server.

The code is this:

#! /bin/bash
echo "Submit Lab$1? \c"

read choice

if [ $choice = "y" ]; then
   echo "Sending to Prof's dropbox..."
   cp -r /path/to/lab$1 /path/to/dropbox
else
   echo "Submission terminated."
fi

Usage for the command is simply “$ submit 1” (1 is a single integer, corresponding to which lab I want to submit)

The bash script appends the entered argument (a single integer) to the necessary filename (a directory), then submits that directory to my prof’s dropbox using cp.

More for the sake of learning than of absolute necessity, I wanted to create a clean prompt that will catch any cp errors (such as file not existing), and be able to output my own error message.

So basically, something like:

echo "Submit lab$1?"

read choice

echo "Send to Prof's dropbox"
cp -rv /path/to/lab$1 /path/to/dropbox

<catch any errors from cp>

if [ cp has errors ]

   echo "Submission failed."

else if [ cp has no errors ]

   echo "Submission successful."

And so on...

I understand that I could use verbose mode, and that cp itself prints out errors, but as stated before, I’m doing this for the purpose of learning error catching in bash and being able to make a clean prompt.

Any help would be appreciated.

Thanks!


Also: I have a similar script which submits labs from my laptop, using scp. Assuming I have this question answered, can I apply the solutions to scp exactly the same way?

EDIT: I don’t want cp errors to output to the shell! The most common error would probably be “cannot stat blah: no such file or directory.” I would like to catch that error, and simply say “Submission failed.”

EDIT #2: jcollado’s answer is correct, the problem is on my part. Did not end the nested if-else with the proper “fi.”

After fixing that up, it worked as intended. Still need to catch the error output but I believe I know where I need to go to figure that out on my own, so this question is answered as far as I’m concerned.

FINAL EDIT: Ah, what the heck – the redirect was so easy I figured I’d put the solution on here. If using Linux, simply add “2>/dev/null” (if you don’t want error messages being saved in any file) after the cp to make “cp -r /first/path /second/path 2>/dev/null“

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:31:37+00:00Added an answer on May 28, 2026 at 8:31 am

    Although you can test $?, you can also test the exit status of a command directly:

    if cp -rv /path/to/lab$1 /path/to/dropbox
    then echo Submission successful
    fi
    exit $?
    

    The errors were already reported to standard error by cp. The exit shown will exit with status 0 (success) if cp was successful; otherwise, it exits with the status that cp exited with.

    Clearly, you can wrap that in a loop if you want to, or you can make it non-interactive (but any exit terminates the script).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to writing bash scripts (and not very good). So it would
Actually very new user to bash.and I am logging into server using ssh sam@xyz.com
I am very new to Bash scripting. I am trying to write a script
I am very new in bash and never coded in before but this task
I have a bash script that tests whether the sftp connection exists, very simple
I'm very new to bash scripting and I'm trying to practice by making this
I am very new to shell scripts and I have a basic image resizing
I'm trying to run multiple instances of a node server using a bash script.
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.