LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   script to call commands from another file (https://www.linuxquestions.org/questions/linux-general-1/script-to-call-commands-from-another-file-647340/)

qipman 06-06-2008 12:05 AM

script to call commands from another file
 
Hello. I am looking for a way to have my script run the commands seen below within the #******** separators from a separate file, rather from within the script itself. Can anyone tell me how to accomplish that?

Thanks,
Lou


########
# Appends the configuration data for each picture in the images.xml file
OLDIFS="$IFS"
IFS=$'\n'
#********
for i in `cat $IN`;do
echo "Creating configuration data for $i"
WC=`wc -l out`
printf ' <product>\n' /r >> $XMLHOME/$NAME
printf ' <itemNumber>'$WC'</itemNumber>\n' /r >> $XMLHOME/$NAME
printf ' <name>Shopping Cart Cover</name>\n' /r >> $XMLHOME/$NAME
printf ' <price>59.99</price>\n' /r >> $XMLHOME/$NAME
printf ' <description>'$i'</description>\n' /r >> $XMLHOME/$NAME
printf ' <pic>'$SM'/'$i'.jpg</pic>\n' /r >> $XMLHOME/$NAME
printf ' <picture>'$BM'/'$i'.jpg</picture>\n' /r >> $XMLHOME/$NAME
printf ' <size>No Options</size>\n' /r >> $XMLHOME/$NAME
printf ' <color>No Shipping Options,</color>\n' /r >> $XMLHOME/$NAME
printf ' </product>\n' /r >> $XMLHOME/$NAME
# printf ' ' \n' /r >> $XMLHOME/$NAME
echo >> $XMLHOME/$NAME
#********

echo "line" >> out
done
IFS="$OLDIFS"
########
# adds final data to xml file:
cat xml-post-cart >> $XMLHOME/$NAME

pixellany 06-06-2008 12:20 AM

I think you are talking about "sourcing" a file.

In a script, if you include:
Quote:

. filename
then that file is made part of your script.

jschiwal 06-06-2008 12:57 AM

The IFS variable is for input not for output.

Most of your script seems to be creating a file that is embedded in the script. Your script doesn't seem to be calling another script however. Look at "here" documents in the bash info manual. (section 3.6.5) That is how embedded scripts & files are usually written. The scripts can contain variables which are expanded, depending on how the terminator is written after the << characters.

Code:

cat >>$XMLHOME/$NAME << -EOF
        <product>
          <itemNumber>$WC</itemNumber>
          <name>Shopping Cart Cover</name>
          <price>59.99</price>
          <description>$i</description>
          <pic>$SM/$i.jpg</pic>
          <picture>$BM/$i.jpg</picture>
          <size>No Options</size>
          <color>No Shipping Options,</color>
        </product>
EOF

Your code could perhaps construct an xml file in parts. First cat'ing a file (>) that contains your standard header. Second adding a <product>..</product> entry for each product entry you need (>>), and finally adding the last line(s) which may contain a closing tags if any from the first header part.

This is how a lot of software was distributed in the past. An installation script would contain a number of embedded files which were created when the script was run.

qipman 06-06-2008 08:15 AM

Thanks for the helpful information. I wrote one of these scripts to create an XML file for each product that a flash shopping cart uses. The end-user simply runs the script (via web) after adding new pictures of their products to the appropriate directory. From there the script processes the info and makes the XML output.

In the end I needed the pricing and option data to be in one file that would be called by all of the individual product scripts so that the end-user would only need to update one file when a price or option change was made (all the products are the same price and use the same options).

The ". file" was exactly what I needed, and I will test creating the XML using the above more-appropriate format.

Many thanks,
Lou


All times are GMT -5. The time now is 12:28 AM.