6 Replies - 698 Views - Last Post: 23 December 2011 - 07:39 AM Rate Topic: -----

#1 Braber01  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 236
  • Joined: 29-November 08

Scramble functon not working in bash

Posted 21 December 2011 - 08:55 AM

Getting error: ./SFL.sh line 64: $'=\E[37m\E[1mLight' command not found
./SFL.sh line 65: '\E[37M\E[1mLight=temp' command not found.

#!/bin/bash
#vim: tabstop=4:softtabstop=4:shiftwidth=4:noexpandtab:autoindent

#function to initlize round one of set for life
function InitUK1 {
 

 #11 lights 3 red, 8 white, play for money then play for time
 #put all the lights into an array
 LIGHTS[1]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
 LIGHTS[2]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
 LIGHTS[3]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
 LIGHTS[4]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
 LIGHTS[5]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
 LIGHTS[6]="$(tput setaf 7;tput bold;)Light$(tput srg0)";
 LIGHTS[7]="$(tput setaf 7;tput bold;)Light$(tput srg0)";
 LIGHTS[8]="$(tput setaf 7;tput bold;)Light$(tput srg0)";
 LIGHTS[9]="$(tput setaf 7;tput bold;)Light$(tput srg0)";
 LIGHTS[10]="$(tput setaf 7;tput bold;)Light$(tput srg0)";
 LIGHTS[11]="$(tput setaf 7;tput bold;)Light$(tput srg0)"
}

funciton InitUK2 {
 #round two of for the rest of your life
 #20 lights 16 white 4 red 4 red and you're dead
 #reset the array
}

function Scramble {
 #scrambles the array 
 local index;
 local lightcount;
 lightcount=${#LIGHTS[*]};
 ((index=lightcount))
 while [[ $index > 0 ]];do
  ((target=RANDOM % index))
  Swap $target $index;
  ((index -= 1))
 done;
}
function Swap {
 #swap array elements
 temp=${LIGHTS[$1]};
 ${LIGHTS[$1]}=${LIGHTS[$2]};
 ${LIGHTS[$2]}=$temp;
}
function DisplayTest {
 #print out the array before an after scrambleing
 for i in ${LIGHTS[*]};do
  echo "$i";
 done;
}
InitUK1;
DisplayTest;
Scramble;
DisplayTest;



i think I figured it out, I forgot to place a $ before a varible

nope, I'm still getting the following errors
./SFL.sh: line 64: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[37m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[37m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[31m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'\E[31m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[31m\E[1mLight=\E[31m\E[1mLight': command not found
./SFL.sh: line 64: $'=\E[31m\E[1mLight': command not found
./SFL.sh: line 65: $'\E[31m\E[1mLight=': command not found


This post has been edited by Braber01: 21 December 2011 - 08:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Scramble functon not working in bash

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Scramble functon not working in bash

Posted 21 December 2011 - 10:57 AM

The file you posted does not have a line 64.

Though on a first skim, it looks like these two lines are the problem:
${LIGHTS[$1]}=${LIGHTS[$2]};
${LIGHTS[$2]}=$temp;



Are you sure you do not just want LIGHTS[$1]=$LIGHTS[$2] and LIGHTS[$2]=$temp here? It does not look like LIGHTS[$1] and LIGHTS[$2] contain valid variable names.
Was This Post Helpful? 0
  • +
  • -

#3 Braber01  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 236
  • Joined: 29-November 08

Re: Scramble functon not working in bash

Posted 21 December 2011 - 11:24 AM

Hmm I ran the debuger, and it's is around the lines you told me, however I think bash is having trouble exchanging the red lights with the white lights, should I make another array to hold the setaf values?

CODE UPDATE
#!/bin/bash
#vim: tabstop=4:softtabstop=4:shiftwidth=4:noexpandtab:autoindent

function InitUK1 {
	#11 lights 8w 3r
	LIGHTS[1]="$(tput setaf 1;tput bold)Light$(tput sgr0)";
	LIGHTS[2]="$(tput setaf 1;tput bold)Light$(tput sgr0)";
	LIGHTS[3]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
	LIGHTS[4]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[5]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[6]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[7]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[8]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[9]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[10]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[11]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
}

function InitUK2 {
	LIGHTS[1]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
	LIGHTS[2]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
	LIGHTS[3]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
	LIGHTS[4]="$(tput setaf 1;tput bold;)Light$(tput sgr0)";
	LIGHTS[5]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[6]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[7]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[8]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[9]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[10]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[11]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[12]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[13]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[14]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[15]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[16]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[17]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[18]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[19]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
	LIGHTS[20]="$(tput setaf 7;tput bold;)Light$(tput sgr0)";
}

#function InitUS1 {

#}

#function InitUS2 {

#}

function Scramble {
	local index;
	local lightcount;
	lightcount="${#LIGHTS[*]}"; #number of elements
	((index=lightcount))
	while [[ $index > 0 ]];do
		((target=RANDOM % index)); #don't need $ because of ((math))
		Swap "${LIGHTS[$target]}" "${LIGHTS[ $index]}";
		((index -= 1))
	done;
}

function Swap {
	local temp;
	"$temp"="${LIGHTS[$1]}";
	"${LIGHTS[$1]}"="${LIGHTS[$2]}";
	"${LIGHTS[$2]}"="$temp";

}

function DisplayTest {
	for i in ${LIGHTS[*]};do
		echo "$i"
	done

}

InitUK1;
DisplayTest;
Scramble;
DisplayTest;



//Down to 1 error
SFL.sh: line 64: Light: syntax error: operand expected (error token is "Light")


This post has been edited by Braber01: 21 December 2011 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 116
  • View blog
  • Posts: 1,660
  • Joined: 28-June 08

Re: Scramble functon not working in bash

Posted 22 December 2011 - 07:33 AM

Don't quote the variable that you are setting. Here:

"$temp"="${LIGHTS[$1]}";
"${LIGHTS[$1]}"="${LIGHTS[$2]}";
"${LIGHTS[$2]}"="$temp";



should be:


temp="${LIGHTS[1]}";
LIGHTS[1]="${LIGHTS[2]}";
LIGHTS[2]="$temp";



You don't quote the variable you are setting, if you are setting an array index instead of evaluating it, don't add the brackets, and finally, it doesn't seem that 1 and 2 are variables, but instead array indexes, so ${LIGHTS[1]} instead of ${LIGHTS[$1]}

This post has been edited by Gorian: 22 December 2011 - 07:36 AM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4886
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: Scramble functon not working in bash

Posted 22 December 2011 - 09:24 AM

Keep in mind what you're swapping. You want to pass locations. Also, assignments normally shouldn't have anything to do with a "$", unless you intent some kind of misdirection.

Just because it's bash, doesn't mean you should just copy paste program...

This should work:
#!/bin/bash

OPT1="$(tput setaf 1;tput bold)Light$(tput sgr0)";
OPT2="$(tput setaf 7;tput bold)Light$(tput sgr0)";

function InitUK1 {
	for i in $(seq 1 3); do LIGHTS[$i]=$OPT1; done
	for i in $(seq 4 11); do LIGHTS[$i]=$OPT2; done
}

function Scramble {
	index="${#LIGHTS[*]}";
	while [[ $index > 0 ]];do
		((target=RANDOM % index)); #don't need $ because of ((math))
		Swap $target $index;
		((index -= 1))
	done;
}

function Swap {
	temp="${LIGHTS[$1]}";
	LIGHTS[$1]="${LIGHTS[$2]}";
	LIGHTS[$2]=$temp;
}

function DisplayTest {
	for i in ${LIGHTS[*]};do
		echo "$i"
	done

}

InitUK1;
DisplayTest;
Scramble;
DisplayTest;


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Scramble functon not working in bash

Posted 22 December 2011 - 02:18 PM

View PostGorian, on 22 December 2011 - 03:33 PM, said:

it doesn't seem that 1 and 2 are variables, but instead array indexes, so ${LIGHTS[1]} instead of ${LIGHTS[$1]}

$1 and $2 are the 1st and 2nd arguments to the function, which he uses as the array indices. So that's definitely intentional. If he used 1 and 2 instead, he'd always be swapping the 1st and 2nd elements of the array and the arguments wouldn't matter.
Was This Post Helpful? 1
  • +
  • -

#7 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 116
  • View blog
  • Posts: 1,660
  • Joined: 28-June 08

Re: Scramble functon not working in bash

Posted 23 December 2011 - 07:39 AM

Aha! That does make sense. Thanks for the correction :P I didn't consider that. Mayhap I should be less tired when answering technical question ><
With that said, I generally reassign variables to the value of the arguments for the purpose of making the code clearer, and then you don't have that issue :P
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1