BashScripts.org
http://bashscripts.org/forum/

Directing output to a subdirectory
http://bashscripts.org/forum/viewtopic.php?f=15&t=1419
Page 1 of 1

Author:  serge [ Wed Jun 29, 2011 7:41 am ]
Post subject:  Directing output to a subdirectory

Hello Everyone!

I am just starting with BASH and have a trivial question. I have a script
Code:
#!/bin/sh

for p in 2 3 4 5 6
do
(echo $p; echo 10; echo 10; echo 2000; echo 1000) | ./finalsim
done


The program saves files in its own directory. How can I tell it to save all output files in an existing subdirectory "data"? Or even better, check the existence of a subdirectory "data", create "data" if it does not exist, and tell the program to save there.

Thanks in advance for your help!
Serge

Author:  jsz [ Wed Jun 29, 2011 12:04 pm ]
Post subject:  Re: Directing output to a subdirectory

What do you mean by « The program saves files in its own directory »? Which files are you refering to?
Why are you using a subshell? And finally, what does the script finalsim do?

Author:  serge [ Wed Jun 29, 2011 12:35 pm ]
Post subject:  Re: Directing output to a subdirectory

finalsim is a cpp program that saves several text files (as an ouput) in the directory in which it is. I want the textfiles to be saved in a subdirectory of the directory in which the program is.

Serge

Author:  jsz [ Wed Jun 29, 2011 7:21 pm ]
Post subject:  Re: Directing output to a subdirectory

Then it's definitely not a bash(1) issue but a C++ one.

Author:  crouse [ Sat Jul 09, 2011 8:12 am ]
Post subject:  Re: Directing output to a subdirectory

jsz wrote:
Then it's definitely not a bash(1) issue but a C++ one.


I think your wrong.

The OP actually asked :
Quote:
The program saves files in its own directory. How can I tell it to save all output files in an existing subdirectory "data"? Or even better, check the existence of a subdirectory "data", create "data" if it does not exist, and tell the program to save there.


It seems like your telling him it can't be done... it can.

# If a sub directory named "data" doesn't exist, if it doesn't create it
Code:
if [ ! -d "data" ]; then
    mkdir data
fi


# Tell the program to save there ...

Method one
# The simplest method would be to move (mv) them after they are created
# Say they all had the extention .final ...then
Code:
(echo $p; echo 10; echo 10; echo 2000; echo 1000) | ./finalsim
mv *.final data/


Method two
# How about you just cd to data and THEN run the program
# Notice the TWO dots in front of your command, as it's in the dir above
Code:
cd data/
(echo $p; echo 10; echo 10; echo 2000; echo 1000) | ../finalsim


Maybe not extremely pretty, perhaps not perfect.....but it works.
The only thing that you may have to take into account with the second method, is if your "finalism" program is getting data from the original directory ;)

Hope that helps.

Author:  jsz [ Sat Jul 09, 2011 12:02 pm ]
Post subject:  Re: Directing output to a subdirectory

Hmm, then the sentence is ambiguous:
« The program saves files in its own directory. How can I tell it to save all output files in an existing subdirectory "data"? ».

Anyway, for this task I'd probably create a temporary file properly thanks to mktemp(1), then use test(0)'s -nt option. It'd be something like:

Code:
#!/bin/sh

tmpfile=$(mktemp -q)
subdir=data

for p in 2 3 4 5 6; do
    printf '%d\n10\n10\n2000\n1000\n' $p | ./finalsim
done

if ! test -d "$subdir"; then
    mkdir "$_" || exit
fi 

for f in ./*; do
    if [ -f "$f" ] && [ "$f" -nt "$tmpfile" ]; then
        mv "$f" "$subdir"
    fi 
done

rm -f "$tmpfile"

Author:  crouse [ Sun Jul 10, 2011 10:48 am ]
Post subject:  Re: Directing output to a subdirectory

Perhaps the sentence isn't as clear as it could have been.

However, there is not only ONE way to do things .... if that were the case, we'd all be programming in python... perhaps why I prefer perl over python myself, but like bash the best still.

So, for me, instead of creating a tempfile that is used only for timestamp comparisons in a dir that others can see, with mktemp, I'd just use touch with a hidden timestamp file for comparisons in the same directory.

Code:
# Create a temp file that's sole purpose is used for timestamp comparison purposes.
# IE: when comparing the timestamp of this file agains all others, you will be able to see which files
# were created AFTER this one...and determine if they should be moved to the data file
touch .mytimestamp

# Create the data dir if it doesn't exist
  if [ ! -d "data" ]; then
      mkdir data
  fi

# Run the finalism program with given options
for p in 2 3 4 5 6
do
(echo $p; echo 10; echo 10; echo 2000; echo 1000) | ./finalsim
done

# Compare all files in the current dir to .mytimestamp.
# if the creation time of any of the files are newer than .mytimestamp
# move those files to /data dir.
for f in ./*; do
    if [ -f "$f" ] && [ "$f" -nt ".mytimestamp" ]; then
        mv "$f" data/
    fi
done

# Remove the hidden file we created for timestamp purposes
rm .mytimestamp


I did like the use of comparing the timestamp to determine if the file should be moved, excellent idea, I had not thought of that option. :)

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com