Check if a variable ends in a particular number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a variable ends in a particular number
# 1  
Old 11-22-2010
Check if a variable ends in a particular number

Hi guys, I am working on a server where there are many users. The user names end in a 1 or a 2. I want to write a bash script that will say which users are in which group and was wondering if I could get some help. The only part I am unsure of is how to check if it ends in the number.

Here's what I have so far.
Code:
for username in 'cat /etc/passwd | cut -d: -f1'
do
     # if $username ends in 1 echo $username is in group 1
     # else if $username ends in 2 echo $username is in group 2
     # else $username is in neither group
done

Thanks!

Last edited by vbe; 11-22-2010 at 02:33 PM.. Reason: code tags please
# 2  
Old 11-22-2010
Code:
case $username in 
  *1) echo $username is in group 1 ;;
  *2) echo $username is in group 2 ;;
  *)  echo $username is in neither group ;;
esac

ps.: there is a difference between ' (single quote) and ` (back quote)

Code:
while read line
do
  username=${line%%:*}
  case $username in 
    *1) echo $username is in group 1 ;;
    *2) echo $username is in group 2 ;;
    *)  echo $username is in neither group ;;
   esac
done < /etc/passwd


Last edited by Scrutinizer; 11-22-2010 at 03:49 PM..
# 3  
Old 11-22-2010
Code:
awk -F: '$1 ~ /.*[1-2]$/ {print "Username " $1 " is in group ", substr($1,length($1))}' /etc/passwd

With sed:
Code:
sed -n 's/\([^:]\([1-2]\)\):.*[1-2].*$/\1 is in group \2/p' /etc/passwd


Last edited by Franklin52; 11-22-2010 at 03:42 PM.. Reason: adding sed solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if a string starts with certain values and ends with numbers

This is very basic. Yet Iam struggling to get the right pattern for my check. Apologize in advance to ask a very lame question. I have to validate if a value of the variable starts with "efgh" and followed by 6 numbers. Var1="efgh234567" The condition Iam trying to achieve is similar to... (6 Replies)
Discussion started by: deepakwins
6 Replies

2. UNIX for Dummies Questions & Answers

UNIX command to check if file name ends with .tar OR if the file is a tar file

Hello Team, Would you please help me with a UNIX command that would check if file is a tar file. if we dont have that , can you help me with UNIX command that would check if file ends with .tar Thanks in advance. (10 Replies)
Discussion started by: sanjaydubey2006
10 Replies

3. Shell Programming and Scripting

How to find a file which are not ends with ".zip" and which are ends with "*.log*" or "*.out*"?

I am new to bash/shell scripting. I want to find all the files in directory and subdirectories, which are not ends with “.zip” and which are contains in the file name “*.log*” or “*.out*”. I know below command to get the files which ends with “.log”; but I need which are not ends with this... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

5. Shell Programming and Scripting

Check if the value is Number

Hi, I have a file with data as given $cat file1.txt 123 234 23e 234.456 234.876e 345.00 I am checking if the values are proper integers using the command. nawk -F'|' 'int($1)!=$1 {printf "Error in field 1|"$0"\n"}' file1.txt This is checking for only integers ( without... (10 Replies)
Discussion started by: ashwin3086
10 Replies

6. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

7. Shell Programming and Scripting

number check in perl

Hi,, this is returning true in all cases..( other than 10 dig number also) what could be wrong?? (2 Replies)
Discussion started by: shellwell
2 Replies

8. Shell Programming and Scripting

Check if a variable is a number - perl

Logic of code if ( $var is a number ) { Do something } else { Do something else } My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help. (3 Replies)
Discussion started by: garric
3 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question