LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 09-02-2004, 09:09 AM   #1
dpaddy
LQ Newbie
 
Registered: Sep 2004
Posts: 3

Rep: Reputation: 0
bash script


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.
 
Old 09-02-2004, 09:17 AM   #2
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Put the command in backticks, like so:
f=`sed 's/\(.*\.ps\)\(\.gz\)*/\1/' $1`
 
Old 09-02-2004, 09:27 AM   #3
dpaddy
LQ Newbie
 
Registered: Sep 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Ok., but i evidently have a problem with sed.

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?
 
Old 09-02-2004, 09:47 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Hi,

The trick is to use backquotes i.e `, upper left of your keyboard, beneath the tilde. So I guess you want something like

#!/bin/sh

f = `echo $1 | sed -e 's/\.ps/\.gz/'`
echo $f


Groetjes,

Kees-Jan
 
Old 09-02-2004, 09:47 AM   #5
dpaddy
LQ Newbie
 
Registered: Sep 2004
Posts: 3

Original Poster
Rep: Reputation: 0
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

PATH=/home/dpaddy/bin:/usr/kde/3.2/bin:/bin:/usr/bin:/usr/maple9.5/bin:/usr/local/bin:/opt/bin:/usr/i386-pc-linux-gnu/gcc-bin/3.3:/usr/X11R6/bin:/opt/blackdown-jdk-1.4.1/bin:/opt/blackdown-jdk-1.4.1/jre/bin:/usr/qt/3/bin:/usr/kde/3.2/bin

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?
 
Old 09-02-2004, 09:51 AM   #6
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Oooops
Sorry,

I'm kind of new to this website. Didn't read the replies yet.

But what you want is probably:
f=`echo $1 | sed -e 's/\.ps\.gz$/\.ps/'`

to take gz of a ps file, or

f=`echo $1 | sed -e 's/\.gz$//'`

to take gz of any file.

In this last case, you could also do
f=`basename $1 .gz`

Sorry again,

Groetjes,

Kees-Jan
 
Old 09-02-2004, 09:57 AM   #7
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Catching up as well....

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)

Groetjes,

Kees-Jan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script Linh Programming 4 04-21-2004 05:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration