You are not logged in.

#1 2009-01-18 23:43:22

whaevr
Member
Registered: 2008-03-17
Posts: 182

[SOLVED]Bash script question...

Is it possible to make a variable equal another variable..but remove something from the second variable first? tongue

example:

Like $*=/home/user/Desktop/test.jpg

I want VARIABLE=$* - /test.jpg

So then I can do something like this in my script

cd $VARIABLE

Anyway I can do that..?

Last edited by whaevr (2009-01-19 00:58:53)

Offline

#2 2009-01-18 23:53:43

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: [SOLVED]Bash script question...

You can cut your variable before using it with cd.

Something like:

[pts/0][shaika-dzari]>VAR='/home/shaika-dzari/haruhi.txt' 
[pts/0][shaika-dzari]>echo $VAR | cut -d/ -f1,2,3;  
/home/shaika-dzari
[pts/0][shaika-dzari]>

BUT, I'm not really great in bash. I'm sure there are others and betters solutions. tongue
Maybe awk or sed...

Offline

#3 2009-01-19 00:00:46

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: [SOLVED]Bash script question...

In general, yes.  And in your specific case:

$ i=/home/user/Desktop/test.jpg
$ j=$(dirname $i)
$ echo $j
/home/user/Desktop

Offline

#4 2009-01-19 00:02:04

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: [SOLVED]Bash script question...

Something like this?

> var1=/home/user/Desktop/test.jpg; var2=${var1%/*}; echo $var2
/home/user/Desktop

See man bash -- "Parameter Expansion" section for more.

EDIT: small fix

Last edited by fwojciec (2009-01-19 00:04:27)

Offline

#5 2009-01-19 00:11:13

whaevr
Member
Registered: 2008-03-17
Posts: 182

Re: [SOLVED]Bash script question...

Alright thanks guys!

Shaika-Dzari yours would work for that specific directory but if I were to use the same script and need it to apply to "/home/user/Desktop/folder/test.jpg" I would need to cut more off it, so it wouldn't work.

tam1138 thanks you gave me what I needed!

fwojciec I didn't try yours..I stopped when tam's worked and yours looks complicated lol

Heres the script I made its for use with the nautilus-actions package.

#!/bin/sh
WOO=$(dirname $*)
NAME="$(zenity --entry --title "Enter Stream" --text "Enter stream number and name to save as")"
cd $WOO
mkvextract tracks $* $NAME | zenity --progress --text="Extracting stream(s)" --pulsate

I do a lot of video editing and was getting tired of doing that in command line over and over...:lol:

Last edited by whaevr (2009-01-19 00:12:10)

Offline

#6 2009-01-19 04:46:18

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: [SOLVED]Bash script question...

You don't want $* there.  That's a list of ALL command-line arguments (and dirname won't behave how you expect/want it to).  $1 is the first argument, $2 is the second, etc.  Even better, at the top, use FILENAME=$1 to put it into a variable with a meaningful name, so that a) it's clear to a user what the command-line arguments mean and b) it's easier to debug later---because sorting out $FILENAME is way easier than $1, let me tell you.

Offline

Board footer

Powered by FluxBB