Need help with bash-script writeing

General discussion about Celestia that doesn't fit into other forums.
Topic author
jim
Posts: 378
Joined: 14.01.2003
With us: 21 years 11 months
Location: Germany

Need help with bash-script writeing

Post #1by jim » 20.09.2003, 22:37

Hi Fridger or some where else,

I need some help. I have some problems with bash script writeing. I want write a script by useing Cygwin and Imagemagic that combines tiles of a virtual texture to a single picture. Also the simple task 'if file exist' can I not solve :(.

Code: Select all

#! /bin/bash
tname="rhea.png"
if -a tname; then
echo "jes"
else
echo "no"
fi


Maybe you can help me what shoud the script do:
- read only TGA files
- The parameters should be x y dx dy
x: start x-number (tile tx_x_y)
y: start y-number
dx: number of x-tiles
dy: number of y-tiles

I hope somewhere can enlighten me ;).

Bye Jens

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 8 months
Location: Hamburg, Germany

Re: Need help with bash-script writeing

Post #2by t00fri » 21.09.2003, 14:55

jim wrote:Hi Fridger or some where else,

I need some help. I have some problems with bash script writeing. I want write a script by useing Cygwin and Imagemagic that combines tiles of a virtual texture to a single picture. Also the simple task 'if file exist' can I not solve :(.

Code: Select all

#! /bin/bash
tname="rhea.png"
if -a tname; then
echo "jes"
else
echo "no"
fi


Maybe you can help me what shoud the script do:
- read only TGA files
- The parameters should be x y dx dy
x: start x-number (tile tx_x_y)
y: start y-number
dx: number of x-tiles
dy: number of y-tiles

I hope somewhere can enlighten me ;).

Bye Jens

Jens,

unfortunately I have virtually zero time, but here is an "emergency kit" to make you understand a few /essentials/:

1) when you define a variable like tname="rhea.png", you must /prepend/ it later with a $ if you want to use /its content/!

2) The 'if ....; then' construct needs a boolean = true/false result to work with. This is provided by the function 'test' or its alias [...]. The option -f <filename> tests whether the filename exists. If yes, then 'if' sees return value true, else false. 'Test' recognizes /many/ different options to test on!! In unix one just finds them by typing 'man test'.

3) Single strings in echo need not be quoted. Only if you were to print
echo "yes, filename is $tname"

Here is the code that works:

Code: Select all

#! /bin/bash

tname="rhea.png"
if test -f $tname; then
echo yes
else
echo no
fi


Hope this helps a little,

Bye Fridger

jamarsa
Posts: 326
Joined: 31.03.2003
With us: 21 years 8 months
Location: San Sebastian (Spain)

Post #3by jamarsa » 22.09.2003, 01:22

t00fri wrote:3) Single strings in echo need not be quoted. Only if you were to print
echo "yes, filename is $tname"


Minor corrections...

There is no need to quote strings in echo to include variables:

$ var=1234
$ echo "$var"
1234
$ echo $var
1234

work exactly the same. The quotes are useful in certain cases, such as maintaining the format of a string with multiple spaces, tabs or certain special characters of different meaning outside the quotes, such as !, #, &, \, <,> ... For example:

$ echo This is a #comment.
and
$ echo "This is a #comment"

give different results. However, some of the special characters have the same behaviour both inside and outside the quotes, such as $,!. The file wildcards work differently in bash than in zsh; if quoted in bash, they are expanded and work as a single string. If not quoted, they work as so many words as files expanded:

Code: Select all

for a in "file*"
do
echo Found $a
done
Found file1 file2

for a in file*
do
echo Found $a
done
Found file1
Found file2


In zsh, they are not expanded if quoted:

Code: Select all

for a in "file*"
do
echo Found $a
done
Found file*

There are two safe ways to avoid the special meaning of these characters, giving the character instead: first, backlash the character (\$ instead of $); second, single-quote them:

Code: Select all

money=0
echo My money is $money
My money is 0
echo My money is \$money
My money is $money
echo 'My money is $money'
My money is $money


The first is better, because you can mix variables and literals within the same string:

Code: Select all

echo "My money is $money\$"
My money is 0$



Sorry for being too academic... :wink:

Topic author
jim
Posts: 378
Joined: 14.01.2003
With us: 21 years 11 months
Location: Germany

Post #4by jim » 22.09.2003, 19:34

Hi Fridger and Jamarsa,

Thank you for your help.

I'm so happy :D. My first 'Bash'-script works unter Cigwin. It produce from a spezified area of a virtual texture (512, tga) a single texture. I was able to build four 16k textures form Waltons 32k Earth and could handle this in Photoshop. The script needs round 30 minutes on my 900MHz/512mb machine to build a 16k texture but with more memory (1GByte and more) it should do this in some minutes. Maybe I should include the DDS to TGA conversion.

Code: Select all

#! /bin/bash
j=$2
k=10000
mkdir t_tmp
while (( j < $2 + $4 )); do
    i=$1
    echo "line " $(( $j - $2 ))
    while (( i < $1 + $3 )); do
        cp tiles/tx_${i}_${j}.tga t_tmp/t_${k}.tga
        (( k++ ))
        (( i++ ))
    done
    (( j++ ))
done
montage -cache 400 +frame +shadow +label -tile ${3}x${4} -geometry 512x512+0+0 t_tmp/t_*.tga area_${1}_${2}_${3}x${4}.tga
rm -r t_tmp

Ok, before you hit on me I know there is absolute no error managment. I know that I must be carfull with the input parameter but so far no one has an interest to use my script I can live with it.

At the moment I have one big problem with the Bash scrip: I don't unterstand the secrets with the brackets. If someone can help me do unterstand when which brackets must be used ?

Bye Jens

jamarsa
Posts: 326
Joined: 31.03.2003
With us: 21 years 8 months
Location: San Sebastian (Spain)

Post #5by jamarsa » 23.09.2003, 00:47

Damn!! I have just lost the text when I was writing another lenghty reply... Too tired to start it again, I will repeat it tomorrow (it's late night here).

(It's the last time I try to write one of these directly in the message body) :(

jamarsa
Posts: 326
Joined: 31.03.2003
With us: 21 years 8 months
Location: San Sebastian (Spain)

Post #6by jamarsa » 24.09.2003, 01:01

Well, guys(&gals...), second try, this time writen in a fault-tolerant editor like vi in my linux PDA, while watching "O' Brother" in the TV - I was not going to miss it ;) .

Another long off-topic post ... :oops:

SQUARE BRACKETS
--------------------

Square brackets, [ ], have two main uses within bash:

a) as an alias of the test command, so useful in unix scripts that it has this 'special' abreviature. You must write a opening bracket, space, the test parameters, space and the closing bracket. This command is worth of a detailed study of its own.

Code: Select all

$ var=5
$ test $var -ge 5 && echo "This is a really big number"
This is a really big number
$ [ $var -ge 5 ] && echo "This is a really big number"
This is a really big number


b) As a file wildcard, together with * and ?. It is, IMO, the most useful of the wildcards (and shamefully forgotten in the poor copy of scripting in DOS command mode). Digressions aside, it specifies a character in a range or list:
[a-f] validates from a to f
[0-4] validates from 0 to 4
[f-a] validates nothing ;)
[s4tHf] validates s,4,t,H and f (but not h, at least in Unix!!)
[^a-f] validates all except from a to f

Now, some samples:

Code: Select all

$ ls texture[2-4]2.dds
texture32.dds
texture42.dds
(sorry,there is no file texture22.dds  8)  )

$ ls texture[^468]2.*
texture12.dds
texture32.dds
texture52.dds
texturea2.dds
textureb2.dds
textureb2.png

$ ls texture[2-48ac]2.dds
texture32.dds
texture42.dds
texture82.dds
texturea2.dds

$ ls texture[2-48ac][1-24].dds
texture31.dds
texture32.dds
texture34.dds
texture42.dds
texture44.dds
texture81.dds
texture82.dds
texturea2.dds


In some commands such as grep, vi, these expressions are used also for pattern recognition (i.e. look for some group of words...). Let's defer it for now.


BRACKETS (BRACELETS?)
------------------------------
As {} are referred too as brackets, let's explain these as well.
They have two uses again (we unixers exploit symbols a lot :twisted: ) :

a) as delimiter of blocks of code. Its main use is for defining functions:

Code: Select all


# This is a really old routine, but I still love it ;-)

prompt()
{
i=0
while true
do
echo "$1(y/n)?\c"
read yn
case "$yn" in
y|Y|yes|YES|Yes|yEs|yES|yeS|YEs|YeS|aye ) return 0;;
n|N|no|NO|No|nO) return 1;;
* ) if [ $i -lt 5 ]
    then echo "You must answer yes or no, please"
    else echo "Answer YES or NO, YOU ID*OT !!!!"
    fi
    ((i=i+1)) ;;
esac

done
}

prompt "Are you sure to delete $1 " && rm $1


b) as delimiter for variable names, when we need to use them concatenated with a literal:

Code: Select all

$ name=Javi
$ echo My name is $nameer
My name is
$ echo My name is ${name}er
My name is Javier

In the former case, we try to show the variable nameer, which is unassigned. The second is correctly displayed.
bracketing isn't necessary when the variable is followed by a punctuation character ([^a-zA-Z0-9_]):

Code: Select all

$ prefix=555
$ echo My phone is $prefix-6543322 # prefix-6543322 is not a valid variable name
My phone is 555-6543322
$ echo My phone is ${prefix}-6543322
My phone is 555-6543322


End of story ( and movie... :( )


Thanks you all for your patience...

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 8 months
Location: Hamburg, Germany

Post #7by t00fri » 24.09.2003, 23:31

Javier,

you forgot the 'double brackets' ((...))

I think those were the ones Jens was perhaps most interested in;-)

Sorry I have 0 time for now, else I would have explained them myself, of course..

Bye Fridger

Topic author
jim
Posts: 378
Joined: 14.01.2003
With us: 21 years 11 months
Location: Germany

Post #8by jim » 25.09.2003, 19:54

Thank you! Javier and Fridger.

Bye Jens


Return to “Celestia Users”