exit a bash command in a script without exiting the script

user836747

I am working on a script that looks at a homework folder (the source files) and if the folder exists, it then looks for a destination folder. If that folder does not exist, it copies the source to the destination.

If the destination exists, it attempts to copy the file from the source to the destination and before doing so, prompt the user first so that it doesn't overwrite work already done by that user.

The output of the command must be in a specific format so I cannot use the standard output of the command as is. I must capture it and reformat it. Then take input (i.e. y/N) in a specific format.

The benefit of doing this, is that the copy command already does things that are helpful. If a file does not exist, it does the copy.

If a file, or several files already exist, it will repeatedly ask "overwrite y/n?" It is this case, that I'm trying to deal with.

I've made it as far as capturing the output of the copy command into a variable and suppressing its normal output so that I can reformat its output and echo that format back to the user.

Next, my thinking is that I need to kill (or otherwise stop the command somehow), then print the output in the my desired format and then re-invoke the command and on the second time, I can echo the y/N prompt in my desired format and the user can then make their choice. Despite searching, I have not been able to figure out/understand how to kill or otherwise stop the command once it is invoked. I've tried kill and various attempts at using SIGINT. Either I just don't understand how to use these, or they are not the right choice. When I try to use them, I get syntax error.

Here is my code:

#!/bin/bash

USAGE="Usage: ./script2.sh [hw-name]"
EINVALID="Invalid homework: $1"
SOURCEDIRECTORY="$PUBLIC/homework/$1"
HOMEWORKDIRECTORY="$HOME/homework/$1"
COPY="$SOURCEDIRECTORY/*.*"

if [ -z "$1" ]; then
    echo $USAGE
    exit
fi

if [ ! -d "$SOURCEDIRECTORY" ]; then
    echo $EINVALID
    exit
fi

if [ ! -d "$HOMEWORKDIRECTORY" ]; then
    echo "making homework1"
    mkdir -p $HOMEWORKDIRECTORY

copyOutput2="$(eval cp -ir $COPY $HOMEWORKDIRECTORY 2>&1 &stop this
 process somehow)"
echo $copyOutput2 #temporary output for testing
Sergiy Kolodyazhnyy

If one really really really needs to have custom cp with custom message, then recompiling the program from source is the way to go, but a bit more sensible approach would be to write out your own message via printf preferably, instead of trying to intercept cp's output, and handle user choices via select dialog. Additionally, if you're going to output error messages, they should go to stderr stream. Finally, *.* is unnecessary and you can just use * glob instead.

Below is the script with a couple edits. Note that you need to uncomment #cp "$item" "$HOMEWORKDIRECTORY" line for the actual copying to occur. For testing purposes replace cp with echo first.

#!/bin/bash

USAGE="Usage: ./script2.sh [hw-name]"
EINVALID="Invalid homework: $1"
SOURCEDIRECTORY="$PUBLIC/homework/$1"
HOMEWORKDIRECTORY="$HOME/homework/$1"
#COPY="$SOURCEDIRECTORY/*.*"

if [ -z "$1" ]; then
    echo "$USAGE" > /dev/stderr
    exit 1
fi

if [ ! -d "$SOURCEDIRECTORY" ]; then
    echo "$EINVALID" > /dev/stderr
    exit 2
fi

if [ ! -d "$HOMEWORKDIRECTORY" ]; then
    echo "making homework1"
    mkdir -p $HOMEWORKDIRECTORY

for item in "$SOURCEDIRECTORY"/*; do
    skip=true
    if [ -f "$HOMEWORKDIRECTORY"/"${item##*/}" ]; then
        printf "%s already exists in %s.\n" "$item" "$HOMEWORKDIRECTORY"

        select choice in  "overwrite" "skip"; do
            case "$choice" in 
                "overwrite")  echo "Overwriting."  
                              break;;
                "skip") skip=true; break;;
            esac
        done
    fi

    if [ "$skip" = "true" ]; then
        continue
    fi
    echo cp "$item" "$HOMEWORKDIRECTORY"

done

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Exit shell script in error handle function without exiting the terminal

分類Dev

Script not exiting on error bash ios

分類Dev

Running a shell script with and without "bash" command

分類Dev

Why can't I get the exit values of a command in this bash script?

分類Dev

Exiting entire bash script from subshell

分類Dev

Exiting bash script for loop on expect script expect error

分類Dev

run command in bash and then exit without killing the command

分類Dev

The paradox of logical AND (&&) and OR (||) in a bash script to check the successful execution of command (exit code of 0 is interpreted as true)

分類Dev

Python: Exiting script

分類Dev

Script exiting on failed nosetests

分類Dev

bash: exit from source-d script

分類Dev

bash script exit codes for dialog menu

分類Dev

Bash pipeline's exit status differs in script

分類Dev

Execute sed command in a bash script

分類Dev

Bash script - unix - command not found

分類Dev

Command works in terminal but not in bash script

分類Dev

Script Command without Junk Character

分類Dev

Using bash variables in perl command in bash script

分類Dev

Python Script not exiting with keyboard Interrupt

分類Dev

How is a bash script executed via its filename as command name with and without shebang?

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Bash script to monitor file change and execute command

分類Dev

Combine sed commands into one command in bash script

分類Dev

How to execute a command within a bash script?

分類Dev

Linux Bash Script, Single Command But Multiple Lines?

分類Dev

bash: Why are () causing error in script but not on the command line?

分類Dev

Bash script using mysql command with variables failing

分類Dev

How to turn Diff command into bash script with prompt

分類Dev

bash script to check website content by curl command

Related 関連記事

  1. 1

    Exit shell script in error handle function without exiting the terminal

  2. 2

    Script not exiting on error bash ios

  3. 3

    Running a shell script with and without "bash" command

  4. 4

    Why can't I get the exit values of a command in this bash script?

  5. 5

    Exiting entire bash script from subshell

  6. 6

    Exiting bash script for loop on expect script expect error

  7. 7

    run command in bash and then exit without killing the command

  8. 8

    The paradox of logical AND (&&) and OR (||) in a bash script to check the successful execution of command (exit code of 0 is interpreted as true)

  9. 9

    Python: Exiting script

  10. 10

    Script exiting on failed nosetests

  11. 11

    bash: exit from source-d script

  12. 12

    bash script exit codes for dialog menu

  13. 13

    Bash pipeline's exit status differs in script

  14. 14

    Execute sed command in a bash script

  15. 15

    Bash script - unix - command not found

  16. 16

    Command works in terminal but not in bash script

  17. 17

    Script Command without Junk Character

  18. 18

    Using bash variables in perl command in bash script

  19. 19

    Python Script not exiting with keyboard Interrupt

  20. 20

    How is a bash script executed via its filename as command name with and without shebang?

  21. 21

    Running a command in a new terminal instance in a bash script

  22. 22

    Bash script to monitor file change and execute command

  23. 23

    Combine sed commands into one command in bash script

  24. 24

    How to execute a command within a bash script?

  25. 25

    Linux Bash Script, Single Command But Multiple Lines?

  26. 26

    bash: Why are () causing error in script but not on the command line?

  27. 27

    Bash script using mysql command with variables failing

  28. 28

    How to turn Diff command into bash script with prompt

  29. 29

    bash script to check website content by curl command

ホットタグ

アーカイブ