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
Solved

Making bash script from bash script

Posted on 2003-12-02
6
814 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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