Hi im new to all this but need help. i have a program that runs in the terminal of redhat 9 "./signalp -t gram+ etc" and this produces an output file called output.txt i need a script that will run the command line automatically after the last one finishes and renames the output file to one of my choosing ie

program run
rename output.txt to 1.txt
run program
rename output.txt to 2.txt
etc
etc

can this be possible?

Recommended Answers

All 9 Replies

Is the program running repeatedly? How/what exactly are you trying to do?

Depending on the ways you wanted to do this, I could hammer out some examples. But, we need more explanation on what you're doing.

The shell script could do this

#!/usr/bin/sh

<put program name here>

if [ -a output1.txt ];
then
mv output1.txt 1.txt
else
echo "file: was not written"
fi

<put next program name here>

if [ -a output2.txt ];
then
mv output2.txt 2.txt
else
echo "file: was not written"
fi
.
.
.
.


When your are finished save the script as a <any name>.sh then at the command line do chmod +x <any name>.sh

You can then run the shell script from the command line

The shell script could do this

#!/usr/bin/sh

<put program name here>

if [ -a output1.txt ];
then
mv output1.txt 1.txt
else
echo "file: was not written"
fi

<put next program name here>

if [ -a output2.txt ];
then
mv output2.txt 2.txt
else
echo "file: was not written"
fi
.
.
.
.


When your are finished save the script as a <any name>.sh then at the command line do chmod +x <any name>.sh

You can then run the shell script from the command line

That's a start, but it's a tad inelegant. Instead of doing that, you could use recursion of some sort with a variable to rename the file, and just reuse a loop to name the files. I'd have to study up a bit on that (my shell scripting's kind of fuzzy), but that'd be more scalable. Like, what if he wanted a file named output35.txt-- he'd have to use that statement 35 times!

#!/usr/bin/sh
#user enters the number of times he wants to run the program

echo "enter a number or amount of times you want the program to run: "
read $number

if [ $number -lt 1 ];
then
echo "error : number must be positive"
exit 1
fi

count = 1

while [ $count -le $number ];
do
<put program name here>
# check if output file exist and has information
if [ -s output.txt ];
then
mv output.txt $count.txt
else
echo "file: was not written $count.txt"
fi
count = count + 1
done

Here is another suggestion

replace the line count = count + 1
with $count = $count + 1

hi i am facing problem in writing a script for rename the filename in one stroke.
i am having so many file of this name " tmp_0456.05o and i want ot convert in to emkt0456.05o.
kindlly help me in writing the unix script.
please give me quick response.

#!/bin/ksh
find /path/to/files -name 'tmp_*' | \
while read filename
do
      tmpfile=${filename#tmp_}
      mv "$filename"   "emkt""$tmpfile"
done

Thanks to Jim mcnamara,
i have tried with your script but finding some problem .
it is not able to find the file. as i mention that i am having so many file with the same name with increse number like as tmp_0432.05o , tmp_0433.05o.......... and i would like to convert in to emkt0432.05o,emkt0433.05o............
i have modify the program little.
kinddly send the solution
#!/bin/csh
find -name 'tmp*' | \
while read filename
do
tmpfile=${filename#tmp_}
mv -f "$filename" "emkt""$tmpfile"
done

find syntax is wrong there has to be a path
find PATH -name

In your case it looks like you should use a . (a dot ) which means the
current working directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.