Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Making bash script from bash script

Posted on 2003-12-02
6
Medium Priority
?
819 Views
Last Modified: 2010-04-22
Dear experts.

I'm having problem making a bash script from another bash script.

The script I want to make is:
======================
#!/bin/sh
if [ ! -x "/opt/kde/bin/startkde" ]; then
   export PATH="$PATH:$opt/kde/bin"
fi
======================

The script that make that script is
======================
#!/bin/sh

# set the variables
PREFIX=/opt/kde  
SCRIPT=/etc/profile.d/kde.sh

# make the script
echo "#!/bin/sh" > $SCRIPT
echo "if [ ! -x \"$PREFIX/bin/startkde\" ]; then" >> $SCRIPT
echo "     export PATH=\"$PATH:$PREFIX/bin\"" >> $SCRIPT
echo "fi" >> $SCRIPT
======================

The result I got was not right, since the $PATH is always expanded to the env variables.
How to prefent it ?


Thanks.


0
Comment
Question by:Kocil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9864588
use single quotes for quoting your script ...
$ gets expanded in double quotes but not single quotes
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 300 total points
ID: 9864608
#!/bin/sh

# set the variables
PREFIX=/opt/kde  
SCRIPT=/etc/profile.d/kde.sh

# make the script
echo '#!/bin/sh' > $SCRIPT
echo 'if [ ! -x \"$PREFIX/bin/startkde\" ]; then' >> $SCRIPT
echo '     export PATH=\"$PATH:$PREFIX/bin\"' >> $SCRIPT
echo 'fi' >> $SCRIPT
0
 
LVL 7

Accepted Solution

by:
glassd earned 500 total points
ID: 9865032
If you are using Bash then you can make the whole thing a bit neater.

{
echo '#!/bin/sh'
echo 'if [ ! -x \"$PREFIX/bin/startkde\" ]; then'
echo '     export PATH=\"$PATH:$PREFIX/bin\"'
echo 'fi'
} > $SCRIPT

or:

echo '#!/bin/sh
if [ ! -x \"$PREFIX/bin/startkde\" ]; then
     export PATH=\"$PATH:$PREFIX/bin\"
fi' > $SCRIPT

The first method allows you to include any command output, not just that from the "echo" command.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Kocil
ID: 9865156
Dear sunny.
The output from your script was

#!/bin/sh
if [ ! -x \"$PREFIX/bin/startkde\" ]; then
     export PATH=\"$PATH:$PREFIX/bin\"
fi

But i think I got the idea.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9865194
Oh ... just noticed what you wanted is slightly different

#!/bin/sh

# set the variables
SCRIPT=/etc/profile.d/kde.sh

# make the script
echo '#!/bin/sh' > $SCRIPT
echo 'PREFIX=/opt/kde' >> $SCRIPT
echo 'if [ ! -x \"$PREFIX/bin/startkde\" ]; then' >> $SCRIPT
echo '     export PATH=\"$PATH:$PREFIX/bin\"' >> $SCRIPT
echo 'fi' >> $SCRIPT
0
 
LVL 5

Author Comment

by:Kocil
ID: 9865216
Glassd, unfortunatelly my script is for a slack package,
so I have to use the spec (sh / ash).

But this seems to works for me

# make the script
echo '#!/bin/sh
if [ ! -x startkde ]; then
    export PATH="$PATH:$PREFIX/bin"
fi' > $SCRIPT

Thanks for both of you.

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question