Register
It is currently Wed Jul 23, 2014 5:53 am

Directing output to a subdirectory


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Wed Jun 29, 2011 7:41 am   

Joined: Wed Jun 29, 2011 7:33 am
Posts: 2
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


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 12:04 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
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?


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 12:35 pm   

Joined: Wed Jun 29, 2011 7:33 am
Posts: 2
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


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 7:21 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Then it's definitely not a bash(1) issue but a C++ one.


Top
 Profile  
 PostPosted: Sat Jul 09, 2011 8:12 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
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.


Top
 Profile WWW  
 PostPosted: Sat Jul 09, 2011 12:02 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
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"


Last edited by jsz on Sun Jul 10, 2011 7:28 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Jul 10, 2011 10:48 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
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. :)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP