Command Line Arguments with getopts Linux Shell Programming BASH Script Tutorial

Поділитися
Вставка
  • Опубліковано 18 гру 2024
  • Webpage - filmsbykris.com
    Support - / metalx1000
    Donate - www.paypal.com...
    Other Channel - / @filmsbykris2.068
    For help: filmsbykris.com...
    FaceBook: / 225113590836253
    This Video Brought to you by:
    Steve Morreale - Patreon supporter

КОМЕНТАРІ • 23

  • @vivarantx
    @vivarantx 2 роки тому

    what If i need to run a command in the script based on the passed named arguments? What is the best way of accumulate them and apply them to that final command on the script?

  • @ggaston85
    @ggaston85 2 роки тому

    Thanks for this video, I was trying to reinvent the wheel, and struggling. What about whole world CLAs?

  • @TheFakeVIP
    @TheFakeVIP 6 років тому

    This is really cool. I know in python, a good module to use is argparser (or argparse, I can't remember) that will sort out switches, both short and long, file names and order of arguments as well.

  • @charlesklein7232
    @charlesklein7232 4 роки тому

    did not understand but you said their was anther video before this i would like to see it what was it called.

  • @git_t0v
    @git_t0v 3 роки тому +1

    Perfect. Thank you!

  • @TheFakeVIP
    @TheFakeVIP 6 років тому +1

    What do you think of using bash as a primary language? I've always seen any kind of shell script as a primitive way to automate things, to a certain extent and I've always thought that you need a propper language like python that was designed to make programs to get things ddone. Clearly your tutorials have proven that a lot can be done in bash, but should it be?,

    • @DigitalMetal
      @DigitalMetal  6 років тому +6

      All Depends on what you're trying to do. I wouldn't want to try and create a First Person Shooter in BASH. Most things as far as system maintenance and automating daily tasks, shell scripts really shine, as that is what they are really for. In the end, it's whatever works for you and what you are trying to do. My only suggestions when it comes to choosing a language, avoid things that are limited to a single platform (such as VB). And also avoid proprietary languages (again things like VB) because you don't want t company to abandon the project and make your code useless in the future.
      It's more about writing good code then it is about the language you write it in.

  • @oko2708
    @oko2708 5 років тому

    Shouldn't you use '==' instead of '=' to compare the $input to ""?

  • @tarifadib
    @tarifadib 5 років тому +7

    if you could put the script in the description, that will be great.

    • @dariangl8707
      @dariangl8707 3 роки тому +1

      he didn t lmao

    • @tarifadib
      @tarifadib 3 роки тому +1

      @@dariangl8707 example 1
      #!/bin/bash
      function main(){
      check $@
      if [ $m ]
      then
      echo "'m' has been chosen."
      fi
      }
      function check(){
      local OPTIND opt i
      while getopts ":cmni:" opt; do
      case $opt in
      i) echo "You chose 'i'"; input="$OPTARG";;
      c) echo "'c' is a good option";;
      m) m=true;;
      n) n_func ;;
      \?) help; exit 1 ;;
      esac
      done
      shift $((OPTIND -1))
      if ["$input" = ""]
      then
      echo "No input file"
      else
      echo "Input is $input"
      fi
      }
      function help(){
      echo "Help. I need somebody"
      echo "Help. Not just anybody"
      }
      function n_func(){
      echo "this is the n_func"
      }
      main $a

    • @tarifadib
      @tarifadib 3 роки тому +1

      ​@@dariangl8707 example 2
      #!/bin/bash
      while getopts ":f:s:" arg; do
      case "$arg" in
      f)
      f="${OPTARG}"
      ;;
      s)
      s="${OPTARG}"
      ;;
      esac
      done
      if [ "$f" = "True" ]
      then
      echo "You entered 'True' for 'f'"
      else
      echo "Defaulting to 'False' for 'f'"
      fi
      if [ ! -z "${s}" ]
      then
      echo "s is $s"
      else
      echo "s is not set"
      fi

  • @ira9186
    @ira9186 3 роки тому

    Thank you. This helped me a lot.

  • @SO-dl2pv
    @SO-dl2pv 6 років тому

    What is the difference between $* and $@

    • @DigitalMetal
      @DigitalMetal  6 років тому +1

      Good Questions. I had to Google it and this is the answer I found: "$* is a single string, whereas $@ is an actual array".

  • @liveasawestruck2450
    @liveasawestruck2450 2 роки тому

    amazing video, really amazing

  • @SO-dl2pv
    @SO-dl2pv 6 років тому +1

    Another great video

  • @simranbanwait
    @simranbanwait Рік тому

    Dude.. you made way too complex, or maybe i'm too stupid to understand this...

  • @TodiDiang
    @TodiDiang 4 роки тому

    Donate me a script lel