0

I am trying to get bash to ask for a user input from read and to check against a file or list of files, then respond with the result from that chesk. This is as far as I have gotten as of yet...

read -p "what are we looking for?" RESP
if [ "$RESP" = sed -n ${some line from inside file} ./file | 
then do "returned variable from file"

else
echo "You need more bash programming" 

I know I am very new here but I cant figure it out, what ever the response is needs to be check against a file (what inside the file)- line by line then return an associated variable of that line

say like

"RESP"=line 3(or any line that matches) of ./this_file 
return "var1" of that line
Do "var1"

fi

Is this the way to do this what would this code look like in simple bash I am so lost please help

example:

line1:"please open google" "var1[open http:www.google.com]

pass "var1" to bash

3
  • Is ./this_file the same as ./file? How is a var1 associated with a line? It would help if you provided specific examples to show what it is that you really want. – John1024 Sep 3 '14 at 1:34
  • ahhh yes, the "var1" should be an associated variable from the line in the file.... i dont know how to do this.. i am assuming it would look like this var1: please open google – Rob Looman Sep 3 '14 at 1:49
  • edited the main post.....looking for a fix to pass an input "resp" to check against the lines of a file then return the "var1" associated with that line..... i hope that is clearer.. then execute the var1... or in this case, open google.com – Rob Looman Sep 3 '14 at 2:03
1

For file, I suggest a format like:

please open google|open http:www.google.com

Here the response is everything before the vertical bar. Your var1 is everything after the vertical bar. (If you had more vars to put on that line, separate them with more vertical bars.)

As for the script, here is a start:

#!/bin/sh
read -p "what are we looking for? " response
action="$(awk -F\| -v r="$response" '$1==r{print $2;exit}' file)"
eval "$action"

The read command gets the input. (It is best practice to use lower case for your shell variables so that you don't accidentally overwrite something important.)

The line with awk extracts the action from the first line in the file that begins with response|. It assumes that everything after that first vertical bar is your command.

The last line above executes action. It does this with eval. The use of eval requires some level of trust. As you refine your design, you should eliminate the use of eval.

Using a different field separator

If we use @ in place of | for the field separator, then file would have lines like:

please open google@open http:www.google.com

The code needs a single change:

read -p "what are we looking for? " response
action="$(awk -F@ -v r="$response" '$1==r{print $2;exit}' file)"
eval "$action"
20
  • hmmmm ok would it be easyer to use speacial characters to delineate between line:"response" and " action" how would we understand the usage of " | "..... the pipe in this context how does that work? i dont understand.... – Rob Looman Sep 3 '14 at 3:40
  • If i say "give me google" and is this suppose to take that as "response" look for it in these files" file1, file2, file 3" then pass that line of everything past " | " into a "var1".....? – Rob Looman Sep 3 '14 at 3:42
  • @RobLooman As used here, | is just a character (not a pipe). I chose it simply because it was unlikely that you would use it as part of a user "response". Any other unusual character could be used in its place, maybe a colon or semicolon or at-sign, etc. – John1024 Sep 3 '14 at 6:34
  • @RobLooman Yes, if the user types in "give me google", then the code selects the stuff past | on the "give me google" line and assigns it the variable action. (I chose action as the name, rather than var1, because it seems more descriptive.) – John1024 Sep 3 '14 at 6:38
  • hmmm i re-read your other posts it all looks good and i will implament it asap, question for you, you said "The line with awk extracts the action from the first line in the file that begins with response|. It assumes that everything after that first vertical bar is your command." i would need this to read the whole file about 10-50 lines is there another way to make it work?.... thank you again – Rob Looman Sep 3 '14 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.