Newbie needs Help with reading files into..

Hello All,

im a newbie in scripting and i need some help

i need a script for reading some files into varibles.

Background

i want to do an

scp localmaschine/localpath/$nameoffile.xml user@server:/path/$nameoffile/*/$nameoffile.xml

i got 20 - 30 files with diferent names i cant change the path and i have to copy it

it would very cool if someone can help me

Would a simple loop not suffice?

maybe?
if its possible? i got one sourcedir with all .xml files on my localhost and i have upload all files to server:/basedir/filenamefolder/filename.xml

The way does not matter

something like

for FILE in $(ls "sourcedir")
do
   scp localpath/$FILE.xml user@server:/path/$FILE/$FILE.xml
done

This is for ksh...

that looks good
thanks mate

---------- Post updated at 02:54 PM ---------- Previous update was at 02:51 PM ----------

but on ls he maybe will pickup the .xml too?

---------- Post updated at 03:47 PM ---------- Previous update was at 02:54 PM ----------

also something like that but only working xD

echo Enter your local directory
read localdir

for FILE in $(ls -1 | cut -d. -f1 "$localdir"); do scp $localdir/$FILE.xml user@server:/path/$FILE/$FILE.xml

if your source directory contains ONLY the .xml files, you can copy the entire directory over recursively:

echo Enter your local directory
read localdir

scp  -r "${localdir}" user@server:/remoteDirPath

@vgersh99 nope that didnt work cause i need the file name for the fully remoteDirpath
$(ls -1 | cut -d. -f1 "$localdir")

---------- Post updated at 07:00 PM ---------- Previous update was at 04:56 PM ----------

ok i got it
for FILE in $(ls -1 "$localdir" | cut -d. -f1 | grep -E '^[A*]'); do scp $localdir$FILE.xml $user@servername:$FILE/$FILE.xml; done

---------- Post updated at 07:19 PM ---------- Previous update was at 07:00 PM ----------

Ok next question xD

         
if check_file test.sh; then
         echo "File exists"
         else
         echo "
         #!/bin/bash

         SOURCEDIR=${manifest}
         if [ -z "$STY" ]; then exec screen -dm -S 'test Import' /bin/bash "$0"; fi
         for i in *; do id=$(echo ${i} | cut -d'_' -f1 | cut -c 2- ); extid=00${id}; /root/test_dir/other_test_file.pl -b ${SOURCEDIR} -m ${extid} -v 2>&1 | tee /root/test_dir/${extid}.log ; done " >> test.sh
         echo created test-Importer
         fi

i want to write this file
but i only want to fill this Varible on the script

SOURCEDIR=${manifest}

the other variables i want to fill later if i running the new created file on server
but if i use this all Variables been removed after creating file

and it looks like that

         #!/bin/bash

         SOURCEDIR=/mnt/f/test5/
         if [ -z  ]; then exec screen -dm -S 'test Import' /bin/bash ./menu.sh; fi
         for i in *; do id=; extid=00; /root/test_import/other_test_file.pl -b  -m  -v 2>&1 | tee /root/test_import/.log ; done

but i need it like that

 #!/bin/bash
         SOURCEDIR=/mnt/f/test5/
         if [ -z "$STY" ]; then exec screen -dm -S 'test Import' /bin/bash "$0"; fi
         for i in *; do id=$(echo ${i} | cut -d'_' -f1 | cut -c 2- ); extid=00${id}; /root/test_dir/other_test_file.pl -b ${SOURCEDIR} -m ${extid} -v 2>&1 | tee /root/test_dir/${extid}.log ; done " >> test.sh
         echo created test-Importer
         fi

A little background: "${...}" is not just any string for the shell but a sort-of command: it means to replace the string ${...} with the value assigned to the variable named ... . If there was nothing assigned before this value is "" (the empty string).

If you want the shell to interpret any character or string literally instead of using the aforementioned interpretation on it there are two ways: quoting and escaping. Quoting means to surround a string by double or single quotes. double quotes only turn off some interpretation mechanisms (i.e. field splitting), but single quoting turns off all of them, also the "variable expansion". Try this to see the difference ( wc counts the number of words) :

x="abc 123"
echo ${x} ; echo ${x} | wc
echo "${x}" ; echo "${x}" | wc
echo '${x}' ; echo '${x}' | wc

The other mechanism is escaping: by prepending a character with a backslash you can remove its meaning for the shell and turn it back into an ordinary character. Try this and notice the difference:

x="abc 123"
echo ${x}
echo \${x}

In your case it could look like this (notice that i use ksh syntax here). Btw., i have straightened out your code, because you don't want such lines not eve in your generated code, much less in your source. Such long lines are hard to read. As a rule of thumb: no line should be longer than 80 characters.

Another thing is: you used echo "..." >> test.sh . This would append the content rather than create a new file when the script runs. I changed that to create/overwrite the file. change the first exec-statement if i assumed wrongly:

if check_file test.sh; then
         echo "File exists"
else
         exec 3>test.sh               # exec 3>>test.sh
         print -u3 - '#!/bin/bash'
         print -u3 - 'SOURCEDIR=${manifest}'
         print -u3 - 'if [ -z "$STY" ] ; then'
         print -u3 - '     exec screen -dm -S 'test Import' /bin/bash "$0"'
         print -u3 - 'fi'
         print -u3 - 'for i in * ; do'
         print -u3 - '     extid="00"$(echo ${i} | cut -d'_' -f1 | cut -c 2- )'
         print -u3 - '     /root/test_dir/other_test_file.pl -b ${SOURCEDIR} -m ${extid} -v 2>&1 | tee /root/test_dir/${extid}.log'
         print -u3 - 'done'
         exec 3>&-
         echo created test-Importer
fi

Another observation: the command exec screen -d... will leave the script because exec replaces the running process (here the script) with the newly called one (here the screen command. Is this intentional?

I hope this helps.

bakunin

Thanks a lot for helping.

Script is working now well

Closed