Skip to content

Instantly share code, notes, and snippets.

@pavithran215
Created December 3, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavithran215/530fbf082ba18df1215f2048af84c886 to your computer and use it in GitHub Desktop.
Save pavithran215/530fbf082ba18df1215f2048af84c886 to your computer and use it in GitHub Desktop.
#!/bin/bash
# c=$(( $1+$2 ))
# echo $c
while getopts "asmd" options;
do
case $options in
a)
echo addition
d=$2
f=$3
echo $(( $d+$f ))
;;
s)
echo subraction
d=$2
f=$3
echo $(( $d-$f ))
;;
m)
echo multiplication
d=$2
f=$3
echo $(( $d*$f ))
;;
d)
echo division
d=$2
f=$3
echo $(( $d/$f ))
;;
*)
echo "invalid option $OPTARG"
;;
esac
done
# ./getOpts.sh -asmd 55 46
# ./getOpts.sh 78 90 -a
# ./getOpts.sh -a 60 70 -sm
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func main() {
argLength := len(os.Args[1:])
fmt.Printf("Arg length is %d\n", argLength)
//it gives the number of arguments passed
//it takes both -flag and values
var argv []string
var arg []int
for _, a := range os.Args[1:] {
if a[0:1] == "-" {
//this if condition will check if the arguments start with "-" i.e flag
// fmt.Printf("function %d is %s\n", i+1, a)
a = a[1:]
//this will slice out the "-" flag
argv = append(argv, a)
// fmt.Printf("values %d is %s\n", i+1, a)
} else {
value, _ := strconv.ParseInt(a, 0, 64)
//string value is converted to int value
arg = append(arg, int(value))
//if the argument doesnt start with flag then it is a value
}
}
// fmt.Println("this are the values", arg)
firstArgumentValue := arg[0]
secondArgumentValue := arg[1]
// fmt.Println(firstArgumentValue)
// fmt.Println(secondArgumentValue)
// fmt.Println(argv)
// res1 := strings.Split(z, ",")
// fmt.Println(res1)
casesStoredInArrays := []string{}
for _, name := range argv {
// fmt.Println(name)
res1 := strings.Split(name, "")
// fmt.Println(res1)
// m := []int{res1}
casesStoredInArrays = append(casesStoredInArrays, res1...)
}
fmt.Println("these are the funtions you choosed", casesStoredInArrays)
for _, getOPts := range casesStoredInArrays {
// fmt.Println(getOPts)
switch getOPts {
case "a":
fmt.Println("addition", firstArgumentValue+secondArgumentValue)
case "s":
fmt.Println("subraction", firstArgumentValue-secondArgumentValue)
case "m":
fmt.Println("multiplication", firstArgumentValue*secondArgumentValue)
case "d":
fmt.Println("division", firstArgumentValue/secondArgumentValue)
default:
fmt.Println("this function is not assigned ", getOPts)
}
}
}
//go build main.go
// below are the test cases you can run
// ./main -asmd 55 46
// ./main 78 90 -a
// ./main -a 60 70 -sm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment