Please use ***
[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do
not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
I'm not going to immediately address the OP, but I just can't stand to see poorly-written scripts, so I was compelled to rewrite it.
See the comments I added for descriptions of the changes and other recommendations.
Code:
#!/bin/bash
# If you have bash and want to use it,
# then specify it explicitly in the shebang.
# Using "/bin/sh" forces the interpreter into posix mode.
# Don't use hard-coded filenames in the code itself.
# Define them in variables at the top of the script.
topdir="/extra/home/remisol"
listfile="$topdir/listord.txt"
outfile="$topdir/ftpord.cmd"
startdir="$topdir/order1"
logdir="$topdir/logs/order1"
backupdir="$topdir/backup/order1"
ftptarget=192.1.2.185
# I'm not entirely sure what this is supposed to do, is this supposed to be
# the PID of the script? The first grep grabs it, but the second removes it?
# In any case, there are probably better ways to get the PID than parsing ps.
# Is this step even necessary?
pid=$( ps -ef | grep "/bin/bash $0" | grep -v -e grep -e $$ )
# In bash, the double-bracket test is recommended for file and string tests.
if [[ -n "$pid" ]]; then
exit 1
fi
# cd into the starting directory.
cd "$startdir"
# Don't use ls to generate lists of files. You can just printf the globbing pattern.
printf '%s\n' * > "$listfile"
# Actually, I really recommend replacing the list file with an array instead.
# listarray=( * )
# You can redirect a whole block of commands at once.
# I used a command grouping, but a here document would also work.
{
echo "user sunftp sunftp"
echo "cd ORDER1"
echo "bin"
} > "$outfile"
# You can also redirect the entire output of a loop at once.
# Always make sure your variables are quoted too!
while read -r myline; do
echo "$myline"
echo "put $myline"
echo "cd ../ORDER3"
echo "put $myline"
echo "cd ../ORDER2"
echo "put $myline"
echo "cd ../BKP_ORDERS"
echo "put $myline"
echo "cd ../ORDER1"
done < "$listfile" >> "$outfile"
# Or if using an array
# for myline in "${listarray[@]}"; do
# ....
# done >> "$outfile"
echo "quit" >> "$outfile"
# Run the generated ftp script.
ftp -n "$ftptarget" < "$outfile"
sleep 10
# Move the files to the backup directory.
while read myline ; do
# cp "$myline" "$logdir"
mv "$myline" "$backupdir"
done < "$listfile"
# If using an array just operate on it directly.
# mv "${listarray[@]}" "$backupdir"
# You should always supply an explicit exit code for the script.
exit 0