Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How does one execute a command (like sed) in a bash script and
have the result (what that command would have printed to stdout)
be assigned to a script variable. For exampe (this does not work,
but perhaps my intent can be guessed by it)
#!/bin/bash
f=sed 's/\(.*\.ps\)\(\.gz\)*/\1/' $1
if [ $1 = $f ] ; then
echo $1
else
echo $f
fi
I want the result of sed to be the value of the variable f,
next I want to compare the value of f with the value of the
first argument to the script. If the result of the comparison
is true (meaning the strings are the same) then I want to
print out the value of the first argument to the script; otherwise
I want to print the value of the variable f.
I understand now that my example should have contained something like
f=`echo $1 | sed 's/\(.*\.ps\)\(\.gz\)*/\1/'`
but this does not accomplish my intent as far as sed goes. I want sed to
look for the pattern something.ps.gz and change it into something.ps
(i.e., remove the trailing .gz). How is that done with sed?
Ok, ok, I am catching on... My problem is with paths, not with sed.
My script is now in final form:
#!/bin/bash
f=`echo $1 | sed 's/\(.*\.ps\)\.gz/\1/'`
if [[ $1 = $f ]] ; then
/usr/bin/ghostview $f
else
gunzip $1
/usr/bin/ghostview $f
fi
And it works if in a Konsole I type the complete path to the name of the
script ( /home/dpaddy/bin/ghostview ). It does not work if I just type
something like ghostview b.ps.gz and leave the complete path to
my ghostview script out. My PATH environment variable is
so I would have thought that my script would be what the system
would execute given the command ghostview. Is this like windows...
do I need to reboot since I am making a change? How do I get
my script to be invoked without typing the complete path?
You can modify your path at any time by typing
export PATH=/your/new/path
after that, your new path will be used in the current console only.
Alternatively, you can edit your ~/.profile or ~/.bashrc or something, and change the path there. In that case, you will need to log out and back in for the changes to activate.
At any time, you can type "echo $PATH" to view your current path, or "which ghostview", to find out which ghostview will be used.
Having said all that, it's probably not a good idea to have two commands on your system with the same name. It is bound to cause confusion (as you are already experiencing). I suggest you rename your script to something else (i.e. mygv or something)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.