Skip to main content
Minor rewording
Source Link
AdminBee
  • 23.6k
  • 25
  • 56
  • 77

From your question it is not sureclear what you want !

anyhowAnyhow it seems that you want the number correspondentcorresponding to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo invalid flag;
            exit 1;
        ;;
    esac
done
echo This is foo:  $foo
#!/bin/bash
foo=0;

while [[ $# -gt 0 ]]; do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo "Invalid flag";
            exit 1;
        ;;
    esac
done

echo "This is foo: $foo"

else ifIf instead you wannawant a mecanismemechanism that treats and validates arguments before being processed

 , you can use something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done
#!/bin/bash

inputf='';
outputf='';
text='';
format='';

while [[ $# -gt 0 ]];do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done

not sure what you want !

anyhow it seems that you want the number correspondent to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo invalid flag;
            exit 1;
        ;;
    esac
done
echo This is foo:  $foo

else if you wanna a mecanisme that treats and validates arguments before being processed

  something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done

From your question it is not clear what you want !

Anyhow it seems that you want the number corresponding to the last argument

#!/bin/bash
foo=0;

while [[ $# -gt 0 ]]; do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo "Invalid flag";
            exit 1;
        ;;
    esac
done

echo "This is foo: $foo"

If instead you want a mechanism that treats and validates arguments before being processed, you can use something like

#!/bin/bash

inputf='';
outputf='';
text='';
format='';

while [[ $# -gt 0 ]];do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done
added 93 characters in body
Source Link
Yunus
  • 1.7k
  • 2
  • 14
  • 19

not sure what you want !

anyhow it seems that you want the number correspondent to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo invalid flag;
            exit 1;
        ;;
    esac
done
echo This is foo:  $foo

else if you wanna a mecanisme that treats and validates arguments before being processed

something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done

not sure what you want !

anyhow it seems that you want the number correspondent to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
    esac
done
echo This is foo:  $foo

else if you wanna a mecanisme that treats and validates arguments before being processed

something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done

not sure what you want !

anyhow it seems that you want the number correspondent to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
        *)
            echo invalid flag;
            exit 1;
        ;;
    esac
done
echo This is foo:  $foo

else if you wanna a mecanisme that treats and validates arguments before being processed

something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done
Source Link
Yunus
  • 1.7k
  • 2
  • 14
  • 19

not sure what you want !

anyhow it seems that you want the number correspondent to the last argument

#!/bin/bash
foo=0;
while (($#));do
    case "${1}" in
        '-n')
            foo=1;
            shift
        ;;
        '-e')
            foo=2;
            shift
        ;;
        '-i')
            foo=3;
            shift
        ;;
        '-k')
            foo=4;
            shift
        ;;
    esac
done
echo This is foo:  $foo

else if you wanna a mecanisme that treats and validates arguments before being processed

something like

#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
    case "${1}" in
        '-i')
            inputf="${2}";
            shift 2
        ;;
        '-o')
            outputf="${2}";
            shift 2
        ;;
        '-t')
            text="${2}";
            shift 2
        ;;
        '-f')
            format="${2}";
            shift 2
        ;;
    esac
done