LinuxQuestions.org
Review your favorite Linux distribution.
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 11-07-2007, 03:17 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
remove the 'd' character from the end of string


An shell script remove the 'd' character from the end of string similar to this:
case "$servicename" in
sshd)
$SERVICE $servicename $action | sed 's/sshd/ssh/'
exit $?
;;
snmpd)
$SERVICE $servicename $action | sed 's/snmpd/snmp/'
exit $?
;;
...

If I want to simplify the script, how to do that?
case "$servicename" in
sshd|sshd)
???
 
Old 11-07-2007, 04:02 PM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
Quote:
Originally Posted by powah View Post
[...]
If I want to simplify the script, how to do that?
case "$servicename" in
sshd|sshd)
???
Code:
case "${servicename%?}" in (ssh|snmp)...
 
Old 11-07-2007, 07:20 PM   #3
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
sed 's/d$//'
will remove the 'd' at the end of the string.

edit:
or just
Code:
foo="sshd"
echo ${foo%d}

Last edited by angrybanana; 11-07-2007 at 07:24 PM.
 
Old 11-07-2007, 08:36 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by powah View Post
An shell script remove the 'd' character from the end of string similar to this:
case "$servicename" in
sshd)
$SERVICE $servicename $action | sed 's/sshd/ssh/'
exit $?
;;
snmpd)
$SERVICE $servicename $action | sed 's/snmpd/snmp/'
exit $?
;;
...

If I want to simplify the script, how to do that?
case "$servicename" in
sshd|sshd)
???
Code:
servicename=sshd
no_d_name=${servicename%d}
printf "%s\n" "$no_d_name"
 
Old 11-07-2007, 10:08 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,541
Blog Entries: 4

Rep: Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430Reputation: 3430
When you said "'d' 'at the end of a string,'" I immediately thought of a regular expression that corresponds to this: /d$/.

Regular-expressions are simply patterns, and they are often enclosed in slashes "/" as-above. They consist of a highly-encoded string of characters which have various meanings: for example, "$" means "end-of-string."

Regular-expressions, or regexes, are a very powerful concept, well-implemented and fast. The "sed" example given above is using the same idea.
 
Old 11-08-2007, 05:31 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,488

Rep: Reputation: 234Reputation: 234Reputation: 234
Code:
$ X=sshd
$ echo ${X}
sshd
$ echo ${X%*d}
ssh
 
Old 11-08-2007, 08:00 AM   #7
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Original Poster
Rep: Reputation: 30
Thank you all for your valuable advice!
 
  


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 05:01 AM
Comparison between character and hexadecimal string.. vishalbutte Programming 2 03-05-2006 08:59 PM
end of file character hyperriven Programming 2 04-08-2004 12:09 AM
end of line character hyperriven Linux - General 1 04-07-2004 09:24 PM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 06:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:05 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