Skip to main content
deleted 101 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic link (and possibly other things)) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing names from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic link (and possibly other things)) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing names from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic link (and possibly other things)) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing names from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    
added 29 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic linkslink (and possibly other things)) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing filesnames from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched broken symbolic links) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing files from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic link (and possibly other things)) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing names from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    
added 164 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched broken symbolic links) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing files from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched broken symbolic links) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing files from "$@"...
    

There are a few ways you can do this, depending on what is convenient and possible.

  • If you can't change the script but it will handle getting no arguments in a graceful manner, then set the nullglob shell option using shopt -s nullglob in the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)

  • If you can't change the script and if it can't handle getting no arguments gracefully, then set the failglob shell option using shopt -s failglob in the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.

  • If you can change the script, then make it test its first argument with [ -e "$1" ] to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched broken symbolic links) and take the necessary actions for this scenario.

    #!/bin/bash
    
    if ! [ -e "$1" ]; then
        # no files given
        exit 1
    fi
    
    # continue processing files from "$@"...
    
  • Similarly to the previous point, but doing the test before calling the script:

    set -- *.txt
    [ -e "$1" ] && ./script "$@"
    
added 101 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading