# problem in shell script

**URL:** <https://community.unix.com/t/problem-in-shell-script/297999>\
**Category:** Shell Programming and Scripting\
**Created:** [October 26, 2011, 8:26am UTC](https://community.unix.com/t/problem-in-shell-script/297999 "2011-10-26T08:26:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ordo\_ordo](https://community.unix.com/letter_avatar/ordo_ordo/32/6_449c533f586939b6e5f246632a46421f.png) [@ordo\_ordo](https://community.unix.com/u/ordo_ordo)\
**Post date:** [October 26, 2011, 8:26am UTC](https://community.unix.com/t/problem-in-shell-script/297999/1 "2011-10-26T08:26:11Z")

</div>

hi every body  
this is my first thread in this forum, i hope find a solution for my problem  
i have to write a script bt i still have some error and i don't know how to correct them

```nohighlight
[mayoura@mayoura final]$ for i in `seq 500 505`; do ./generateur_tache $i tache$i.txt; nprocs=$i; copt$i=`cat tache$i.txt | ./copt.awk` ; ./generateur_machine $(( $i * 5 / 100 )) $copt$i machine perf ; clpt$i=`./lpt $i $(( $i * 5 / 100 ))` ; ./gensched -t tache$i.txt -m machine$i -p perf$i -b 0 -g D -h D -o progdyn$i ; cmax$i=`cat progdyn$i | ./cmax.awk`; echo ` $clpt$i - $copt$i`" "` $cmax$i - $copt$i` ; done
bash: copt500=74.778: commande inconnue...
bash: clpt500=la: commande inconnue...
Invalid performance file
cat: progdyn500: Aucun fichier ou dossier de ce type
bash: cmax500=0: commande inconnue...

```

did any one have a solution please 😕  
thank u

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [October 26, 2011, 9:33am UTC](https://community.unix.com/t/problem-in-shell-script/297999/2 "2011-10-26T09:33:23Z")

</div>

Usually when I have some issues not understanding why my great one liner didn't work, I put it in a script - one process per line, so I can add echoes or use set -x...

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [October 26, 2011, 9:33am UTC](https://community.unix.com/t/problem-in-shell-script/297999/3 "2011-10-26T09:33:50Z")

</div>

What do you expect the following scripts to do `./generateur_tache ./copt.awk /generateur_machine ./gensched ./cmax.awk `  
Also could you format your script in a readable fashion (ie rewrite as a script rather than a command line hack.

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [October 26, 2011, 9:38am UTC](https://community.unix.com/t/problem-in-shell-script/297999/4 "2011-10-26T09:38:24Z")

</div>

Hi! And welcome to the forum...

First thing about your code, try to make it more readable, at least when posting here and also, try to "translate" the error messages to english, with error messages in other languages, only people that knows your language will be able to help you! =o)

I think the problem with your script, check the comment I put on it, below:

```nohighlight
for i in `seq 500 505`
do
	./generateur_tache ${i} tache${i}.txt
	nprocs=${i}
	
	copt${i}=`cat tache$i.txt | ./copt.awk` # What you are trying to do here? Is it a typo? A variable name with a variable? it is not possible, as I know it... And why you need it?
	
	./generateur_machine $(( ${i} * 5 / 100 )) ${copt}${i} machine perf
	clpt$i=`./lpt $i $(( $i * 5 / 100 ))`
	./gensched -t tache${i}.txt -m machine${i} -p perf${i} -b 0 -g D -h D -o progdyn${i}
	cmax${i}=`cat progdyn${i} | ./cmax.awk`
	echo `${clpt}${i} - ${copt}${i}`" "` ${cmax}${i} - ${copt}${i}`
done

```

---

<div class="post-metadata">

**Author:** ![ordo\_ordo](https://community.unix.com/letter_avatar/ordo_ordo/32/6_449c533f586939b6e5f246632a46421f.png) [@ordo\_ordo](https://community.unix.com/u/ordo_ordo)\
**Post date:** [October 26, 2011, 9:44am UTC](https://community.unix.com/t/problem-in-shell-script/297999/5 "2011-10-26T09:44:23Z")

</div>

thank you for replying  
so for the first code it will generate a file in wich i line  
copt is a variable calculated from this file (the sum of the line divided by an other variable) this copt will be used in generateur-machine  
in bref it's a scheduling problem to resolve , copt is the optimal makespan if u know about it  
sorry for my poor english  
and thank u again 🙂

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [October 26, 2011, 11:59am UTC](https://community.unix.com/t/problem-in-shell-script/297999/6 "2011-10-26T11:59:25Z")

</div>

Hi, have you checked the comments I added in your code:

```nohighlight
copt${i}=`cat tache$i.txt | ./copt.awk` # What you are trying to do here? Is it a typo? A variable name with a variable? it is not possible, as I know it... And why you need it?

```

---

<div class="post-metadata">

**Author:** ![ordo\_ordo](https://community.unix.com/letter_avatar/ordo_ordo/32/6_449c533f586939b6e5f246632a46421f.png) [@ordo\_ordo](https://community.unix.com/u/ordo_ordo)\
**Post date:** [October 26, 2011, 1:08pm UTC](https://community.unix.com/t/problem-in-shell-script/297999/7 "2011-10-26T13:08:03Z")

</div>

yes i have all reay checked it and i answered , in fact i need this variable to use it in next command  
as i have said the first code will generate a file with $i line (supposed as tasks to schedule each id of task with it's duration inside) the second file will take the variable copt (which is the optimal completition time calculated in copt.awk) and will generate a file containing the machines to do this tasks the number of machine is calculated from 5% of $i and the indisponibility are calculated from copt , then two scheduling algorithm will be tested , the first one in lpt(longest processing time) the completition time will be extracted from the terminal which is clpt and the second one from gensched which is a dynamic programming from which will be extracted cmax and then will be shown on the terminal the difference between copt, cmax and clpt

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [October 26, 2011, 1:19pm UTC](https://community.unix.com/t/problem-in-shell-script/297999/8 "2011-10-26T13:19:24Z")

</div>

Ok... I can tell you that wherever you have a variable name defined by another variable value (eg

```nohighlight
copt${i}=`cat tache$i.txt | ./copt.awk`

```

), the script will fail because this does not work on shell scripts.

Maybe you can replace it by an array, then you will be able to get the value back.

By the errors reported, what I commented above, caused them:

```nohighlight
bash: copt500=74.778: commande inconnue...
bash: clpt500=la: commande inconnue...
...

```

I hope it helps...

---

<div class="post-metadata">

**Author:** ![ordo\_ordo](https://community.unix.com/letter_avatar/ordo_ordo/32/6_449c533f586939b6e5f246632a46421f.png) [@ordo\_ordo](https://community.unix.com/u/ordo_ordo)\
**Post date:** [October 26, 2011, 1:30pm UTC](https://community.unix.com/t/problem-in-shell-script/297999/9 "2011-10-26T13:30:31Z")

</div>

a friend recommended me to do with arrays but i'm not really pro in shell programming so i don't really know how to proceed 😢

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [October 26, 2011, 1:37pm UTC](https://community.unix.com/t/problem-in-shell-script/297999/10 "2011-10-26T13:37:07Z")

</div>

Nothing special:

```nohighlight
unset testArray
testArray[0]="foo"
testArray[1]="bar"
testArray[2]="foobar"

arrayCountPos=0
arrayLastPos=${#testArray[@]}

while [${arrayCountPos} -lt ${arrayLastPos}]
do
     echo "Array pos[${arrayCountPos}] - Value: [${testArray[${arrayCountPos}]}]"
     arrayCountPos=`${arrayCountPos} + 1`
done

```

This is the way I am used to do, but there are always more than one way to do it. =o)

I hope it helps.

---

<div class="post-metadata">

**Author:** ![ordo\_ordo](https://community.unix.com/letter_avatar/ordo_ordo/32/6_449c533f586939b6e5f246632a46421f.png) [@ordo\_ordo](https://community.unix.com/u/ordo_ordo)\
**Post date:** [October 26, 2011, 1:50pm UTC](https://community.unix.com/t/problem-in-shell-script/297999/12 "2011-10-26T13:50:46Z")

</div>

thank you, i will try it 🙂
