shell script help: sorting, incrementing environment variables and stuff


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script help: sorting, incrementing environment variables and stuff
# 1  
Old 05-08-2008
shell script help: sorting, incrementing environment variables and stuff

First: me == noob. Whats a good resource for shell script info cause I'm having trouble finding good info.

I'm writing a shell script to automate the setup of a flash 'page flip'.

My current code is below.

the page flip takes an xml file of format
HTML Code:
<content>
<pages src="/path/to/image.jpg" preload="true"></pages>
etc
</content>
I want my shell script to:

1. make folders ./xml and ./pages
2. copy the jpegs to the "./pages/param2" folder
3. Read a directory of jpegs (passed as param1)
4. build the xml file listing the files SORTED!
(eg <page src="file1" preload="true"></page>
<page src="file2" preload="true"></page>...
<page src="file10" preload="false"></page>)
5. only set preload=true for the first X pages
6. package ./pages and ./xml into a tar then transfer it to my web server

Some of the jpegs whose filenames need to be used in the xml file, have names like:
PrefixIndex.jpg
eg, where BRISGUIDE_JAN08 is the Prefix:

BRISGUIDE_JAN08.jpg
BRISGUIDE_JAN0802.jpg
BRISGUIDE_JAN0803.jpg
BRISGUIDE_JAN0804.jpg
etc

So to get it to sort properly in the xml file(eg 2.jpg comes after 1.jpg, not after 10.jpg), i was pulling the BRISGUIDE_JAN08 off and using sort -n to arrange them by their Index.
but then I end up with a file called .jpg cause the first file doesn't have an index...
so i thought I'd add a 0 to the start of the file. This is fine, a little ugly but the question is:

maybe there is a way to get the sort command to sort a list like:

Code:
page.jpg
page1.jpg
page10.jpg
page2.jpg
page3.jpg

into
Code:
page0.jpg
page1.jpg
page2.jpg
page3.jpg
page10.jpg

I guess I could just add the prefix back on to the file after I've written the xml file in numeric order....

PLUS I can't get the counting variables working for the preload up to X working.
Is there an alternative to environment variables for counting in shell scripts?

Maybe this would be better in a different shell? CSH?

Thanks!

Code:
#!/bin/sh
if [ $# -ne 3 ]; then
        echo;
        echo Usage: $0 source_jpegs_folder name [prefix_to_remove]
        echo EXAMPLE: $0 ./jpegs 01_2008_SEQ BRISGUIDE_JAN08
        echo;
        exit 127
fi

automator_path=/YYY/Projects/Pageflip/ag_automator; export automator_path;
ag_id=$2; export ag_id;
number_to_preload=6; export number_to_preload;
ag_counter=0; export ag_counter;
mkdir $automator_path/pages;
mkdir $automator_path/xml;

cd $1;

for each in *.jpg; do
        cp "$each" $automator_path/pages/$(echo $each|sed "s/ /_/g")
done

echo "<content width=\"283\" height=\"595\" hcover=\"false\" transparency=\"true\">" > $automator_path/xml/$ag_id.xml

cd $automator_path/pages/

mkdir $automator_path/pages/$2;

for f in *.jpg; do
        mv $f $automator_path/pages/$ag_id/
done

cd $automator_path;

cd pages/$ag_id;

for h in *.jpg; do
        mv $h 0${h#$3};
done


for f in `ls *.jpg | sort -n`; do
        if [ $ag_counter -lt $number_to_preload ]; then
        echo "<page src=\"/pages/$2/0$f\" onLoad=\"true\" ></page>" >> $automator_path/xml/${ag_id}.xml
        else
        echo "<page src=\"/pages/$2/0$f\" onLoad=\"false\" ></page>" >> $automator_path/xml/${ag_id}.xml
        fi
        "$ag_counter" = "$ag_counter"+1;
        export "$ag_counter";
done

echo "</content>" >> $automator_path/xml/$ag_id.xml;

cd $automator_path

tar -cvzf $ag_id.tar.gz xml/$ag_id.xml pages/$ag_id;
scp ./$ag_id.tar.gz admin@XXX.com:~/vhosts/XXX.com/httpdocs/work;
wait;

# 2  
Old 05-08-2008
Hi.

One solution for the sort:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate sort numerically on portion of field.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) sort
echo

cat >data1 <<EOF
page1.jpg
page10.jpg
page2.jpg
page.jpg
page3.jpg
EOF

sort --key=1.5n data1

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
sort (coreutils) 5.2.1

page.jpg
page1.jpg
page2.jpg
page3.jpg
page10.jpg

See http://www.tldp.org/LDP/abs/html/index.html for a tutorial on scripting with many, many examples ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Challenge with sh script using environment variables to check for file.

Hi All Thanks for reviewing my question. I have a sh script where I used an environmental variable for the directory for the file I need to check to ensure before executing a process. I have confirmed the permissions and I can find the file if I use a hard coding of the directory. This is a... (5 Replies)
Discussion started by: rstojkovic68
5 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Run script through cron with user environment variables

Hi everyone, I wrote a script that is supposed to be run by cron on a daily basis. It works just fine if I run it manually, but due to a lack of environment variables (which are available during my user session but not when cron runs the script) it keeps failing to run successfully. Here's the... (2 Replies)
Discussion started by: gacanepa
2 Replies

4. Shell Programming and Scripting

Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following: csh -c 'source ~/.cshrc; exec bash' # works perfectly (cat ~/.cshrc; echo exec bash) | csh # not working And, using sed, I successfully retrieved the environment variables from ~/.cshrc sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc but now... (6 Replies)
Discussion started by: royalibrahim
6 Replies

5. Shell Programming and Scripting

Difficulty with incrementing Variables and using the results in a If/else statement

Environment: BASH version: GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10) Copyright (C) 2007 Free Software Foundation, Inc. OS: Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. ... (4 Replies)
Discussion started by: os2mac
4 Replies

6. Shell Programming and Scripting

Using Datastage environment variables in Unix script

Hi All, I am using ETL tool Datastage and is installed on Linux environment. Few environment variables are set in datastage. Now my requirement is to use those environment variables in a unix script. Is there any option I can do it? Sugeestions from people working on datastage and linux... (1 Reply)
Discussion started by: bghosh
1 Replies

7. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Emergency UNIX and Linux Support

Problem setting environment variables from script

Hi all! I know that environment variables can be set on the .bashrc file, but I need to set them from a sh script. I saw a lot of websites that teach this but it doesn't work for me. #!/bin/sh DEKTOP=$DESKTOP=:/home/rrodrigues/Desktop export DESKTOP if I do echo $DESKTOP returns me... (10 Replies)
Discussion started by: ruben.rodrigues
10 Replies

9. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

10. Programming

incrementing variables in C++

Hello, what is the result of the below, and how does it work? int i = 5; cout << i++ * ++i << endl; cout << i << endl; (12 Replies)
Discussion started by: milhan
12 Replies
Login or Register to Ask a Question