Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions.

so heres my code:

#!/bin/bash

##functions

memory () {
free -m
}

space () {
    df -h
}


ip () {
    arp
}


user () {
    whoami
}



#'eingabemen�'

echo " hello how would you like to proceed? "
echo " 1) check memory "
echo " 2) check free space "
echo " 3) ip info "
echo " 4) user info "
echo " 5) nothing i'd just like to exit "

##read

read input;

case $input in 

    1)  echo " you've selected check memory..1 sec pls "
echo -e " free memory: " ; free -m;;
    2)  echo " you've selected check free space..1 sec pls "
echo -e " free space: " ; df -h;;
    3)  echo " you've selected ip info . 1 sec pls "
echo -e  " ipinfo: " ; ifconfig;;
    4)  echo " you've selected user info..1 sec pls "
echo -e " benutzer: " ; whoami;;
     5)  exit
esac 

i came this far, :o now my problem is, i want to open my shell script by

for example and i want the function "space" which ive created to show me the info I need.

can anyone help me trying to fix this. thanks guys

Hello,

I think that you should launch your script in this way

./test.sh 2

or other number that do you want.

maybe I didnt write correctly and you missunderstood me, i'm sorry.
what i want to do is. when i open my script my normal menu appers. now i just want to make it quicker and enter the function after the test.sh path i already defined before and get my data.
greets

Hi,
your script defines some functions but never uses them. When you process the userinput you call some commands directly that you use in the functions .
About your problem: command line arguments are stored in the positional parameters. They are referenced by a number which reflects their position. The first argument is $1 , the second $2 ,... The special parameter $# holds the number of arguments passed.
I would check if $# is 1 and if that's true assign the corresponding number to the variable input depending on what $1 holds.

1 Like

If you want to get a value from the command line, use the positional parameters, $1, etc., not 'read'.

case $1 in 
    1|memory)  echo " you've selected check memory..1 sec pls "
               echo -e " free memory: " ; free -m;;
    2|space)  echo " you've selected check free space..1 sec pls "
              echo -e " free space: " ; df -h;;
    3|ipinfo)  echo " you've selected ip info . 1 sec pls "
               echo -e  " ipinfo: " ; ifconfig;;
    4|user)  echo " you've selected user info..1 sec pls "
             echo -e " benutzer: " ; whoami;;
     5|exit)  exit
esac
1 Like

You could try this:

#!/bin/sh
#
#	Vars & Functions
#
	MENU="memory space ip user"
	memory () {
		free -m
	}

	space () {
	    df -h
	}


	ip () {
	    arp
	}


	user () {
	    whoami
	}
#
#	Menu
#
	if [[ -z $1 ]]
	then	echo "Select which info to display"
		select ENTRY in $MENU;do break;done
	else	ENTRY=$1
	fi
#
#	Execute
#
	$ENTRY

Hope this helps

NOTE:
This only works as long as the menu entry to choose from is named identical as the function to execute

1 Like