Jump to content

/bin/bash "For" loop statement not working?


Recommended Posts

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 by Tazintosh
Link to post
  • 2 weeks later...

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 post

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 post

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 by Tazintosh
Link to post

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 post

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 post
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 post

 

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]}!"
Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...