0

I am trying to create a while loop that checks my data file for a particular string $username and if a duplicate of it is found in the data file, a number is added to the end of it. This is what I have and for some reason I get an error "too many arguments" for the line containing the while loop. any help would be greatly appreciated.

    tput cup 3 12
    echo "Enter the first name of the user: "
    tput cup 3 47; read firstName
    tput cup 4 12; echo "Enter the last name of the user: "
    tput cup 4 45; read lastName
    username=${firstName:0:1}${lastName:0:4}
    i=0
    while [ -n $(grep -q -F "$username" capstonedata.txt) ]
    do
            let i=$i+1
            username=$username$i
    done
    userdata=${firstName}":"${lastName}":"${username}
    echo $userdata | tr [a-z] [A-Z] >> capstonedata.txt
1
  • I have come to realize a few of my mistakes but it still is not doing what I mean for it to ' i=0 while [ -n $(grep -q -F "$username" capstonedata.txt) ] do let i=$i+1 username=$username$i done ' Commented Dec 9, 2017 at 0:47

1 Answer 1

1

You need to decide whether the result of your command is an empty string, not the command it self. Try

while [ -n $(grep -q -F "$username" capstonedata.txt) ]

Edit

A common error is adding an extra dollar sign when you assign values to variables in shell script. Never use anything like

$username=...

if you want to assign something to the variable called username. Remove the dollar sign before it. On the other hand, you need to use i=$i+1

Besides that, I don't really understand what you are trying to achieve by using username=$username[i].

The following script may address your needs or may not, depending on whether I have got your intention right.

#!/bin/bash

(cat <<EOF
$username
EOF
)|(while read line
do
        cnt=`grep "\b$line[0-9]*\b" capstonedata.txt| wc -l`
        if [ $cnt -gt 1 ]
        then
                echo $line$cnt
        else
                echo $line
        fi
done
)

Since OP has further clarified his need, the script can be made much simpler.

cnt=`grep "\b$username[0-9]*\b" capstonedata.txt|wc -l`
if [ "$cnt" -eq "0" ]; then
    cnt=
fi
username=$username$cnt

Explanation: "$username[0-9]*" matches the $username, with optional digits after it, "\b" matches word boundaries (but this seems to only work with egrep on some systems although it works perfectly fine on my system, you can use grep "\<$username[0-9]*\>" as a safer option) so that you don't accidentally match the line "abcd" when $username is "ab". wc -l counts the number of lines of matches, so if the username has appeared once, the result will be 1. The back-ticks are used to assign the result of a command to a variable, which is in this case $cnt. The if-clause sets $cnt to empty string if it is 0 (i.e. there's no matching in the file). The last line is just appending $cnt to the original user name. So if there is one line that matches the regex the result will be $username with a 1 appended. Precisely what you need.

Sign up to request clarification or add additional context in comments.

24 Comments

thank you, the error no longer appears, however the data is not being inserted into the file, when the command is run it just stops after the loop
There are other issues in your script but since you only mentioned the error in the if statement, I did not modify the other parts. I will modify my answer to address your needs.
I have realized a few of my mistakes but it still just stops after the loop and does not continue the script i=0 while [ -n $(grep -q -F "$username" capstonedata.txt) ] do let i=$i+1 username=$username$i done
basically what i am trying to do is figure out whether or not there is a duplicate of the username already in the file, and if there is, append a number to the end of it, increasing by an increment of 1. for example if there is a username aaron, the username aaron1 will be created instead of a duplicate. if there is already a username aaron1, aaron2 will be created. I will edit my post with the whole script.
Since you used grep -F in your code, I suppose that $username contains more than one line, is it the case?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.