1

I have a bash script I am working on where I need to pass two arguments into a function. When I run the script everything works fine, except when it parses through the file I pass to it, the result I always get is Association (this is written in the text file). Can you advise what I would need to do so that it parses the correct info, which should be what I would type in as uid? I wrote in the input 123456789 because that's in the text file, but again I am getting Association as the result instead.

echo "Please enter the UID: ";
read uid

echo "Please enter server file: ";
read server
uidAssoc(){
            arg1=$1
            arg2=$2
            for arg1 in $(cat ~/jlog/"$2"); do awk 'match($i, $2){ print substr($i, RSTART, RLENGTH)} ' ~/jlog/"$2";done;}
    uidAssoc "$uid" "$server"
    exit

1 Answer 1

1

The closing brace of the function requires whitespace in front of it (reference). Formatting your code more readably will help.

uidAssoc(){
    arg1=$1
    arg2=$2
    for arg1 in $(cat ~/jlog/"$2"); do
        awk 'match($i, $2){ print substr($i, RSTART, RLENGTH)} ' ~/jlog/"$2"
    done
}

Some questions for you:

  • Why do you assign arg1 and arg2 but never use them?
  • What is $i in your awk script? (since i is unset, that will eventually evaluate to the whole line)
  • Are you aware that the $2 inside single quotes is different from the $2 outside of single quotes?
Sign up to request clarification or add additional context in comments.

1 Comment

For arg1 and arg2, they are used as local variables for the paramters. For $i, I used the match function in awk and it required some arguments. As for $2, I am aware of the differences betweetn single and double quotes. I am still kind of novice so I had to tweak the script around a bit.

Your Answer

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