Tazintosh Posted June 30, 2013 Share Posted June 30, 2013 (edited) Hi folks, I manage to create the following script in php that's working. The aim is to extract and use multiple parameters from a single {query}. Here's a simplified example: $query = explode(' ', '{query}'); $elemA = $query[0]; $elemB = $query[1]; $elemC = $query[2]; echo "{$elemA}{$elemB}{$elemC}"; I would like to do the same in bash but cannot manage to get it work. Here's my try: This one is working outside Alfred, directly for my Terminal. Basically, it also automatically detect any space as a separator. query="{query}" i=1 for param in $query; do case $i in 1) elemA=$param;; 2) elemB=$param;; 3) elemC=$param;; esac i=$[i+1] done echo "${elemA}${elemB}${elemC}" From all my tries, I've not been able to enter the "for" loop statement… Would you have any idea? Thanks in advance, Cheers, Édouard. Edited June 30, 2013 by Tazintosh Link to comment
Andrew Posted July 12, 2013 Share Posted July 12, 2013 Bump? I'm going to move this to the workflow help forum, you may get more response there Link to comment
dfay Posted July 12, 2013 Share Posted July 12, 2013 I set this up with "Keyword to Script to Notification" and it runs perfectly fine as far as I can tell - echoed the output to the notification as expected. I edited the case statement to change elemC to a fixed string, as a way of testing that the for loop definitely ran, and it worked that way as well. What makes you think it isn't working? Link to comment
Tazintosh Posted July 14, 2013 Author Share Posted July 14, 2013 What makes you think it isn't working? Because it does not echo anything ^^ Link to comment
jdfwarrior Posted July 14, 2013 Share Posted July 14, 2013 Because it does not echo anything ^^ The issue here is that Alfred passed you a list of files (assuming thats what the multiple items are that you are trying to send) as a tab delimited list. By default, using a for loop in bash will iterate over a spar separated string, not a tab delimited string. Try using IFS to set the delimiter and trying again. For instance... files="{query}" IFS=" " set $files for file in $files do echo "$file" >> output.txt done This code worked perfectly fine for me with a multi file selection. I used IFS to set the delimiter to a tab, used 'set' to make it take notice of that on the $files array I created, the iterated over it. Link to comment
Tazintosh Posted July 15, 2013 Author Share Posted July 15, 2013 (edited) Thanks for your help David, but I'm afraid I'm not following you. I'm not dealing with files at all. What I'm trying to do is to extract multiple parameters from one query. Basic and stupid example with the following query: • query = "John 192.168.1.25 johndoe" (without quotes) • We can see that the query is three group of word, space separated (these are my 3 parameters). • My echo would be: "Hello John, your IP is 192.168.1.25 and your login is johndoe" Hope that's help. Thanks! Edited July 15, 2013 by Tazintosh Link to comment
jdfwarrior Posted July 15, 2013 Share Posted July 15, 2013 Thanks for your help David, but I'm afraid I'm not following you. I'm not dealing with files at all. What I'm trying to do is to extract multiple parameters from one query. Basic and stupid example with the following query: • query = "John 192.168.1.25 johndoe" (without quotes) • We can see that the query is three group of word, space separated (these are my 3 parameters). • My echo would be: "Hello John, your IP is 192.168.1.25 and your login is johndoe" Hope that's help. Thanks! I assumed you were trying to pass multiple files into a workflow and that, that was what you were trying to iterate over. In either case, a for loop still works. query="{query}" for element in $query do echo $element done I created a workflow that had a keyword, a run script, and a large type output. I entered the code above in the run script area and was able to see the 3 words I entered in as a parameter to the workflow shown on 3 lines (as expected) in large type. There isn't an issue with running a for loop in Alfred. There must be something going on with your config or the script that you are running. Perhaps you could share a little more about what you're doing? Share code, sample input, etc? Link to comment
Tazintosh Posted July 15, 2013 Author Share Posted July 15, 2013 So, if my query contains 3 elements, how do I echo them? My tries with "Hello $element[0], your IP is $element[1] and your login is $element[2]" are not working Link to comment
jdfwarrior Posted July 16, 2013 Share Posted July 16, 2013 So, if my query contains 3 elements, how do I echo them? My tries with "Hello $element[0], your IP is $element[1] and your login is $element[2]" are not working If they are run through a for loop as shown above, then you would just echo $element, because at each iteration, the $element variable is updated with the value of the next element in the array Link to comment
Tazintosh Posted July 16, 2013 Author Share Posted July 16, 2013 Thanks for your support David, but I still don't get how I could — with your example — build the following example of echo: How can I choose a specific parameter and use it multiple times, at different places on my echoed string? • query = "John 192.168.1.25 johndoe" (without quotes) • echo: "Hello John, your IP is 192.168.1.25 and your login is johndoe. Remember to use the IP 192.168.1.25 as DHCP with fixed IP and respect the case for johndoe. See you soon John!" Thanks Link to comment
jdfwarrior Posted July 16, 2013 Share Posted July 16, 2013 Thanks for your support David, but I still don't get how I could — with your example — build the following example of echo: How can I choose a specific parameter and use it multiple times, at different places on my echoed string? • query = "John 192.168.1.25 johndoe" (without quotes) • echo: "Hello John, your IP is 192.168.1.25 and your login is johndoe. Remember to use the IP 192.168.1.25 as DHCP with fixed IP and respect the case for johndoe. See you soon John!" Thanks My example wasn't attempting as much to accomplish that, it was simply to prove that, contradictory to the title, for loops do work. To solve your issue, as mentioned above, try using IFS. This example performs what you are asking about. query="John 192.168.1.25 johndoe" IFS=' ' read -a array <<< "$query" echo "Hello ${array[0]}, your IP is ${array[1]} and your login in ${array[1]}. Remember to use the IP ${array[1]} as DHCP with fixed IP and respect the case for ${array[2]}. See you soon ${array[0]}!" Tazintosh 1 Link to comment
Tazintosh Posted July 16, 2013 Author Share Posted July 16, 2013 You just made my day! Thanks for the support David! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now