If statement arguments


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers If statement arguments
# 1  
Old 10-26-2019
If statement arguments

I'm stuck on a particular problem and need some guidance. I have a file with a name and a phone number in it (teledir.txt). I need to do a $# in a separate script to take a positional parameter and check to see if it is in the file. To quote the question:



Code:
If one argument is supplied, check to see if it starts with a digit, then search for it in the teledir.txt file. If it doesn't exist, echo "Doesn't Exist".


If argument starts with a letter, search for it in the file, If it doesn't exist, echo "Name does not exist" 



If two arguments are supplied, set the search_pattern to be “name: number”¯, and search it in the file. 
  • If it is not found, echo “Adding entry”¯, and append to the file/variable.
  • Otherwise, echo that “Entry exists”¯

I am only two weeks into learning the command line prompt. This is the code I have gotten so far but I am stuck on where to go from here. Not sure if this will even work. Any pointers please!!!


Code:
#!/bin/bash

name=$1
number=$2


if [ $# -eq 0 ]; then
        echo "Two arguments are expected in the form of ./scriptname [name number]"
fi

if [ $# -eq 2 ]; then
        grep -qiw "$name\|$number" teledir.txt
        echo "$name or $number exists"
fi

# 2  
Old 10-26-2019
In bash you can check whether a variable starts with a number like so:
Code:
if [[ $somevar == [0-9]* ]]; then

You can check if it begins with a letter like this:
Code:
if [[ $somevar == [[:alpha:]]* ]]; then

# 3  
Old 10-26-2019
Quote:
Originally Posted by Eric7giants
Code:
if [ $# -eq 2 ]; then
        grep -qiw "$name\|$number" teledir.txt
        echo "$name or $number exists"
fi

Code:
if [ $# -eq 2 ]; then
        if grep -qiw "$name\|$number" teledir.txt; then
            echo "$name or $number exists"
        fi
fi

# 4  
Old 10-26-2019
Thanks for all of the help. I truly appreciate it. I am running into one more issue. When I enter a positional parameter of a name that I know is in the file. It still outputs as name does not exist. This is the code I have so far:


Code:
#!/bin/bash

name=$1
number=$2

if [ $# -eq 0 ]; then
        echo "Two arguments are expected in the form of ./scriptname [name number]"
fi


if [ $# -eq 1 ]; then
        if grep -qiw "$name | $number" teledir.txt; then
                echo "$name exists"
        else
                echo "Does Not Exist"
        fi
fi

# 5  
Old 10-26-2019
Quote:
if grep -qiw "$name | $number" teledir.txt; then
Code:
if grep -qiw "$name\|$number" teledir.txt; then

and with this design, 2 parameters are needed and not equal to an empty string
Otherwise
Code:
if [ $# -eq 1 ]; then
    if grep -qiw "$name" teledir.txt; then
        echo exists
    else
        echo no exists
    fi
fi

--- Post updated at 21:16 ---

Code:
#!/bin/bash
if [ $# -lt 2 ]; then
        echo 2 parameters required
        exit
else
        if [ -z "$1" -o -z "$2" ]; then
                echo The parameter cannot be an empty string
                exit
        fi
        if [ -n "${1//[[:alpha:]]}" -o -n "${2//[0-9-]}" ]; then
                echo Invalid parameters
                exit
        fi

fi

if grep -qiw "$1\|$2" teledir.txt; then
        echo "$1 or $2 exists"
else
        echo "Does Not Exist"
fi


Last edited by nezabudka; 10-26-2019 at 02:41 PM..
# 6  
Old 10-26-2019
The issue I'm running into is that if I enter a number as a parameter, it functions correctly, but when I enter a name, nothing happens.


Code:
#!/bin/bash

name=$1
number=$2

if [ $# -eq 0 ]; then
        echo "Two arguments are expected in the form of ./scriptname [name number]"
fi


case $name in
        $1)

                if [[ $1 == [[:alpha]]* ]]; then
                        if grep -qiw "$name" teledir.txt; then
                                echo "$name exists"
                        else
                                echo "Name  does NOT exist"
                        fi


                elif [[ $1 == [0-9]* ]]; then
                        if grep -qiw "$name" teledir.txt; then
                                echo "$name  exists"
                       else
                                echo "Number does NOT exist"
                       fi
                fi


esac

# 7  
Old 10-26-2019
This is how I would approach the problem shown in post #1, making use of bash features like "parameter expansion / Remove matching suffix pattern" and testing empty "conditional expressions". It could seriously benefit from some polishing, e.g. combining grep runs, variable quoting, error handling, and upfront argument compliance checks. Try
Code:
name=$1
number=$2
FN=teledir.txt

case  $# in
        1)      if [ ${name%%[[:digit:]]*} ]
                  then  if ! grep -iw "$name:" $FN
                          then  echo "Name doesn't exist."
                        fi
                  elif [ ${name%%[[:alpha:]]*} ]
                    then if ! grep -iw "$name" $FN
                           then echo "$name doesn't exist."
                         fi
                  fi ;;
        2)      grep -qi "$name: $number" $FN &&
                  echo "$name: $number exist" ||
                  echo "$name: $number" >> $FN
                ;;
        *)      echo "Two arguments are expected in the form of ./scriptname [name] [number]";;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Arguments in usage statement

Hello, I have a question regarding the usage statement of a script. I have 2 parameters "--pto" and "--pto_list". To start the script I will need one of them. Both together are not possible. How this would be printed out within a usage statement? My suggestion would be: Usage:... (4 Replies)
Discussion started by: API
4 Replies

3. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

creating printf statement using user arguments

I am writing a script in bash and want to perform the operation I check number of arguments and make a print statement with the passes arguments If I pass 3 arguments I will do printf "$frmt" "$1" "$2" "$3"If I have 4 arguments I do printf "$frmt" "$1" "$2" "$3" "$4"etc (4 Replies)
Discussion started by: kristinu
4 Replies

5. Shell Programming and Scripting

case statement for different cmd arguments

Hello friends, I have a boubt passing different arguments at a time for any one option in below code. I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified. for i and r - install and re-install -... (4 Replies)
Discussion started by: pd2
4 Replies

6. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

7. Shell Programming and Scripting

Too many arguments?

I can't find anything wrong with this line of code, it works when there is one file in the directory but more than one i get a "too many arguements2 error if ; then am i missing something? (3 Replies)
Discussion started by: Alendrin
3 Replies

8. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

9. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

10. Shell Programming and Scripting

if statement - how can I do 2 arguments?

I am new to shell, and I am trying to do a if statement like the following: if ; then basically it works fine if both arguments of the if are met, however the next elif is: elif ; then if the conditions of the elif are met, then it says "final1.sh: line 67: [: too many arguments" ... (6 Replies)
Discussion started by: Darklight
6 Replies
Login or Register to Ask a Question