Solved

Making bash script from bash script

Posted on 2003-12-02
6
810 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
  • 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 75 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 125 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now