LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2006, 09:15 PM   #1
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Rep: Reputation: 15
Remove rightmost character from bash


I have a string value that has % sign on the rightmost portion, how do I remove it from bash script? One exaple is this one, 98%, I only want to get 98.TIA.
 
Old 07-27-2006, 09:54 PM   #2
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Rep: Reputation: 15
To specifically remove the last %sign (from say a variable named $var:

Code:
var="98%"
var=$(echo ${var%\%})
Remember, no spaces on either side of the equals sign. If you're interested in learning more then "man bash" and do a search for Parameter Expansion.

Hope that helps.
 
Old 07-27-2006, 10:06 PM   #3
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
theFoo,
thanks,it worked perfectly.
 
Old 11-16-2007, 10:47 PM   #4
kamahl
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
Hi, I'm trying to do the same thing, except that I'm removing a line break. Is there any possible way to do it?
 
Old 01-25-2008, 11:19 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
In my Bash 2.05b:
Code:
var="98%"
var=${var%\%}
works equally well.


kamahl,
A terminal new line in a variable?
How did it get there?
What harm is it causing?
 
Old 01-26-2008, 03:48 AM   #6
kamahl
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
Code:
#! /bin/bash
killall `cat .desktop`
files=(/usr/lib/xscreensaver/*)
N=${#files[@]}
((N=RANDOM%N))
randomfile= `${files[$N]}`
`echo $randomfile` > .desktop
`echo $randomfile` -root
The above code *should* launch a screensaver as a desktop background, but it instead runs: "/usr/lib/xscreensaver/xmatrix
-root"

it opens the screensaver in a window, and says "-root is not a command"
 
Old 01-27-2008, 11:51 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
I can't follow your code very well -- what directory are you running this in, I get:
Code:
$ cat .desktop
cat: .desktop: No such file or directory
There is no /usr/lib/xscreensaver/ on my SimplyMEPIS (& KDE) 3.3.2 system. What distro & DE are you using?


I especially don't understand how your last 2 lines work. I have no idea why you are backticking an echo command. Try this:
Code:
$randomfile -root
Edit, added:
If you prefer your code gratuitously complicated, this works too:
Code:
$(`echo $randomfile` -root)

Last edited by archtoad6; 01-27-2008 at 11:58 AM. Reason: add add'l "Code:" block
 
Old 01-28-2008, 07:31 AM   #8
kamahl
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by archtoad6 View Post
I can't follow your code very well -- what directory are you running this in, I get:
Code:
$ cat .desktop
cat: .desktop: No such file or directory
User made file in home directory
Quote:
There is no /usr/lib/xscreensaver/ on my SimplyMEPIS (& KDE) 3.3.2 system. What distro & DE are you using?
Have you got the xscreensaver program installed?
Ubuntu + Xfce4
Quote:
I especially don't understand how your last 2 lines work. I have no idea why you are backticking an echo command. Try this:
Code:
$randomfile -root
Edit, added:
If you prefer your code gratuitously complicated, this works too:
Code:
$(`echo $randomfile` -root)
Because I didn't know a more efficent way existed? I'm kinda new at this.

Last edited by kamahl; 01-28-2008 at 07:32 AM. Reason: typo
 
Old 01-28-2008, 08:13 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by kamahl View Post
User made file in home directory
Crucial info that you left out, along with its contents. I suspect it's a list of screen savers that might be active.


Quote:
Originally Posted by kamahl View Post
Have you got the xscreensaver program installed?
No. It's not stock on my system, is it on yours?


Quote:
Originally Posted by kamahl View Post
Ubuntu + Xfce4
Which Ubuntu, incl. ver. #?
Is it really Ubuntu, w/ the xubuntu-desktop package installed; or actually Xubuntu?


Quote:
Originally Posted by kamahl View Post
Because I didn't know a more efficent way existed? I'm kinda new at this.
As you can see, a variable whose value is a program name can be used to call the program.

Although I think that solves your problem, you still haven't shown a variable w/ a detectable & harmful new line.
 
Old 01-29-2008, 12:08 AM   #10
kamahl
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by archtoad6 View Post
Crucial info that you left out, along with its contents. I suspect it's a list of screen savers that might be active.
Nope, file made shortly later on in the script. a blank file should work.
Quote:
No. It's not stock on my system, is it on yours?
nope. I prefer it to gnome-screensaver though
Quote:
Which Ubuntu, incl. ver. #?
Is it really Ubuntu, w/ the xubuntu-desktop package installed; or actually Xubuntu?
Xbuntu Gutsy
Quote:
As you can see, a variable whose value is a program name can be used to call the program.

Although I think that solves your problem, you still haven't shown a variable w/ a detectable & harmful new line.
[/quote]

The newline is still in there causing the same problems.
Code:
./changedesktop.sh: line 8: -root: command not found
the problem arises from $randomfile containing a linebreak, I either need to change the selection method, or strip it out.

I tried both methods of executing the variable that you suggested, but they both failed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 02:13 PM
Bash alias: issue with character riotxix Linux - General 3 02-17-2006 10:00 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM


All times are GMT -5. The time now is 05:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration