i using getopts but when i type in the argument it not working. Is there anything wrong wif my script? bcos when i enter bash filename -P -S , i should see "hello" msg but i don.

#!/bin/bash

while getopts "PS" opt;
do
	if [ $# -eq 2 ]; then
		
		case "$opt" in

		PS)  echo "hello"
		;;
		
		\?)  echo "wrong!!"
		esac
	else
		echo "Required 3 Args!!!" 
	fi
done

Hey There,

You're almost perfect there. The preferred way woud be for you to have a

P)

and and

S)

switch for each option, but you could do a range:

[PS])

instead. Your script now is looking for a literal "PS" argument.

Hope that helps :)

, Mike

thanks eggi!!! but now i have another problem which is when i wanted to entered another argument it will print out the last entry of the file. but when i always enter the argument it just wont show the last entry. Is it that my logic of my code is wrong?

#!/bin/bash

while getopts "PSps" opt;
do
	if [ $# -eq 2 ]; then
		
		case "$opt" in

		[PS])  echo "hello"
		;;
		p) sed -n '$p' /etc/passwd
                ;;
                s) sed -n '$p' /etc/shadow
                ;;
		\?)  echo "wrong!!"
		esac
	else
		echo "Required 3 Args!!!" 
	fi
done

i written this script that will allow to add user when supply 3 compulsory argument which is -A -G -Q. When the 4th argument supply it will only show my file last entry and not prompting for adding new user. But i have problem when my 4th argument supply it just wont print out my file last entry. How should i place it so that it will only show my file last entry when 4th argument is supply?

while getopts "AGQaq" opt;
	do		
		if [ $# -eq 3 ]; then

		case "$opt" in
		[AGQ]) 
			prompt for user input
	                ....
                        ....
		;;
		p)	sed -n '$p' /etc/passwd;
		;;
		
		s)	sed -n '$p' /etc/shadow;	
		;;
			  
		
		\?)  	echo "Wrong Arguments!!!"
		esac
		
	done

Hey again,

This is just a guess since I'm a little confused about what you need from the question, but, if you are using getopts to parse arguments that require arguments, like if -p requires a line number e.g. -p 5)

You would need to add a colon after each opt that requires an argument

while getopts "PSps" opt;

would become this:

while getopts "PSp:s:" opt;

so getopts would know that if someone entered -p or -s, they would need to supply an additional value for that argument.

Am I way off base? ;)

Best wishes,

Mike

Sorry tht my question is unclear. It require user to enter 3 compulsary arugment in order for the program to run and it will prompt user to enter some data in.
Example:
bash filename -P -G -S

But if the user enter -p as the 4th argument it will only print out the last entry of the text file.
Example:
bash filename -P -G -S -p

The problem i have now is that when i keep in the 4 argument it will prompt me to enter new data which is not i wanted. i only wanted it to print out last entry of my text file when -p is enter.

Hey Again :)

Hopefully this will help. I just tooled around with original script for a bit and this will complain if less than 3 args, accept your forth and/or fifth arg and print the last line without any interaction on FreeBSD 4.1

#!/bin/bash

while getopts "PGSps" opt;
do
        if [ $# -ge 3 ]; then

                case "$opt" in

                [PS])  echo "hello"
                ;;
                p) sed -n '$p' /etc/passwd
                ;;
                s) sed -n '$p' /etc/shadow
                ;;
                \?)  echo "wrong!!"
                esac
        else
                echo "Required 3 Args!!!"
        fi
done

Cheers,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.