Solved

Making bash script from bash script

Posted on 2003-12-02
6
815 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 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
Technology Partners: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Avoiding pop-up when starting a linux scrip/shell 5 112
generating rpm using noarch parameter 1 47
updating the repos in yum 4 121
„dd“ vs „ddrescue“ 28 263
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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