LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-06-2008, 01:21 AM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
ease bash/sh coding: Has someone right(,), left(,), and len(,) scripts for /usr/bin ?


Hi,

Like in basic, right(,), left(,), and len(,) are very very useful.

Has someone right(,), left(,), and len(,) scripts for /usr/bin ?

Thanks
Happy sh tux

would you have the same for :

right.sh
Code:
#!/bin/sh
# echo  $1 is the string
# echo $2 is the number of chars
...?
??
??
left.sh
Code:
#!/bin/sh
# echo  $1 is the string
# echo $2 is the number of chars
...?
??
??

Last edited by frenchn00b; 04-19-2009 at 07:30 AM.
 
Old 11-06-2008, 01:32 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Not sure if this is what you want, but here are 3 ways to get a string/var length
http://unstableme.blogspot.com/2008/...ngth-bash.html
 
Old 11-16-2008, 08:14 PM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
I dont know maybe there is some packages ffor the
/usr/bin ????
already

certainly

those could help a looooooooooooooooot
 
Old 11-16-2008, 08:30 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Why bother?

In Linux, you've probably got (already installed): Perl, PHP, Ruby, Python and more. Any of these full-featured programming languages "go on forever." And you can use any one of them in any command, thanks to "#!command_processor_name" shebang.

So... in my opinion... if you are using "bash scripting" to do anything other than the most-trivial ... "you're missing the point."
 
Old 11-17-2008, 01:51 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

why search for separate packages? The functions come already shipped with the bash:
Code:
VAR="abcdefg"
echo len=${#VAR}
echo left[3]=${VAR:1:3}
echo right[3]=${VAR:$((${#VAR}-3))}
Jan
 
Old 04-19-2009, 07:30 AM   #6
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by jan61 View Post
Moin,

why search for separate packages? The functions come already shipped with the bash:
Code:
VAR="abcdefg"
echo len=${#VAR}
echo left[3]=${VAR:1:3}
echo right[3]=${VAR:$((${#VAR}-3))}
Jan

would you please have the same for :

right.sh
Code:
#!/bin/sh
# echo  $1 is the string
# echo $2 is the number of chars
...?
??
??
left.sh
Code:
#!/bin/sh
# echo  $1 is the string
# echo $2 is the number of chars
...?
??
??
 
Old 04-19-2009, 01:28 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by jan61 View Post
Code:
VAR="abcdefg"
echo len=${#VAR}
echo left[3]=${VAR:1:3}
echo right[3]=${VAR:$((${#VAR}-3))}
Code:
echo right[3]=${VAR: -3}
 
Old 04-21-2009, 04:17 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

I still don't understand, why you want to get shell scripts for one liners - you'll create a new subshell every time you call these scripts. The simple use of the shell builtins would prevent you from such resource wasting. But if you really want to do this:
Code:
jan@jack:~/tmp> cat left.sh
VAR=$1
LEN=$2
echo ${VAR:0:$LEN}
jan@jack:~/tmp> cat right.sh
VAR=$1
LEN=$2
echo ${VAR: -$LEN}
jan@jack:~/tmp> ./left.sh WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> ./right.sh WORKS_IN_SHELL 5
SHELL
thx to Hko for the hint to the shorter right command. As you see, there was a little bug in the left command (starting at 1, not at 0).

Jan

EDIT: A better way instead of writing shell scripts would be to define aliases or functions in your login scripts or system wide login scripts, for example:
Code:
jan@jack:~/tmp> cat myfunctions
function left() {
  local VAR=$1
  local LEN=$2
  echo ${VAR:0:$LEN}
}

function right() {
  local VAR=$1
  local LEN=$2
  echo ${VAR: -$LEN}
}
jan@jack:~/tmp> . ./myfunctions
jan@jack:~/tmp> left WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> right WORKS_IN_SHELL 5
SHELL

Last edited by jan61; 04-21-2009 at 04:31 PM.
 
Old 04-21-2009, 04:31 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by jan61 View Post
I still don't understand, why you want to get shell scripts for one liners - you'll create a new subshell every time you call these scripts. The simple use of the shell builtins would prevent you from such resource wasting.
I was wondering too, but the only thing I could think of was that the OP was asking for a way to do it in sh (i.e. not bash). Since those builtins are bash-specific.

Quote:
Originally Posted by jan61 View Post
thx to Hko for the hint to the shorter right command.
You're welcome :-)
FYI: The space between ':' and '-' is needed.

Last edited by Hko; 04-21-2009 at 04:35 PM.
 
Old 04-21-2009, 04:52 PM   #10
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by Hko View Post
I was wondering too, but the only thing I could think of was that the OP was asking for a way to do it in sh (i.e. not bash). Since those builtins are bash-specific.
This would be an explanation - but why didn't he tell us???


Quote:
Originally Posted by Hko View Post
FYI: The space between ':' and '-' is needed.
*g* - Now I know ;-) I remember: I tried the negative number (without the space!) and was really angry, because it didn't work. The first thing I did with your line was to remove the (totally uncool) space and I was astonished again %-/ But I'm able to learn ...

Jan

BTW: Some weeks ago I noticed, that some old fashioned Korn shells (for example in Sun - ehm, Oracle - Solaris10) now include a subset of bash features. One could look, if the mentioned features are part of the new ksh's.

Last edited by jan61; 04-21-2009 at 04:58 PM.
 
Old 04-21-2009, 10:25 PM   #11
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by jan61 View Post
Moin,

I still don't understand, why you want to get shell scripts for one liners - you'll create a new subshell every time you call these scripts. The simple use of the shell builtins would prevent you from such resource wasting. But if you really want to do this:
Code:
jan@jack:~/tmp> cat left.sh
VAR=$1
LEN=$2
echo ${VAR:0:$LEN}
jan@jack:~/tmp> cat right.sh
VAR=$1
LEN=$2
echo ${VAR: -$LEN}
jan@jack:~/tmp> ./left.sh WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> ./right.sh WORKS_IN_SHELL 5
SHELL
thx to Hko for the hint to the shorter right command. As you see, there was a little bug in the left command (starting at 1, not at 0).

Jan

EDIT: A better way instead of writing shell scripts would be to define aliases or functions in your login scripts or system wide login scripts, for example:
Code:
jan@jack:~/tmp> cat myfunctions
function left() {
  local VAR=$1
  local LEN=$2
  echo ${VAR:0:$LEN}
}

function right() {
  local VAR=$1
  local LEN=$2
  echo ${VAR: -$LEN}
}
jan@jack:~/tmp> . ./myfunctions
jan@jack:~/tmp> left WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> right WORKS_IN_SHELL 5
SHELL
Thank you !! I was floooding of learning those day, and I put that program on the side. Thank you !!
 
Old 04-22-2009, 11:03 AM   #12
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by jan61 View Post
Moin,

I still don't understand, why you want to get shell scripts for one liners - you'll create a new subshell every time you call these scripts. The simple use of the shell builtins would prevent you from such resource wasting. But if you really want to do this:
Code:
jan@jack:~/tmp> cat left.sh
VAR=$1
LEN=$2
echo ${VAR:0:$LEN}
jan@jack:~/tmp> cat right.sh
VAR=$1
LEN=$2
echo ${VAR: -$LEN}
jan@jack:~/tmp> ./left.sh WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> ./right.sh WORKS_IN_SHELL 5
SHELL
thx to Hko for the hint to the shorter right command. As you see, there was a little bug in the left command (starting at 1, not at 0).

Jan

EDIT: A better way instead of writing shell scripts would be to define aliases or functions in your login scripts or system wide login scripts, for example:
Code:
jan@jack:~/tmp> cat myfunctions
function left() {
  local VAR=$1
  local LEN=$2
  echo ${VAR:0:$LEN}
}

function right() {
  local VAR=$1
  local LEN=$2
  echo ${VAR: -$LEN}
}
jan@jack:~/tmp> . ./myfunctions
jan@jack:~/tmp> left WORKS_IN_SHELL 5
WORKS
jan@jack:~/tmp> right WORKS_IN_SHELL 5
SHELL

working

Code:
#!/usr/bin/bash

function left() {
  local VAR="$1"
  local LEN="$2"
  echo ${VAR:0:$LEN}
}

function right() {
  local VAR="$1"
  local LEN="$2"
  echo ${VAR: -$LEN}
}

function trim() {
  eval $1=` echo $1 |    sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'   `
}

TEST="  sta   rtKLJK LJKLJKLJKLend "
right "$TEST" 6
left "$TEST" 6
echo $TEST
Code:
$ sh test
KLend
sta
sta rtKLJK LJKLJKLJKLend
 
Old 04-23-2009, 02:33 PM   #13
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by frenchn00b View Post
Code:
...
function trim() {
  eval $1=` echo $1 |    sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'   `
}
This does not work on the right side:
Code:
jan@jack:~> VAR="  123  456    "
jan@jack:~> VAR1=`echo "$VAR" | sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'`
jan@jack:~> echo "[$VAR] [$VAR1]"
[  123  456    ] [123  456    ]
Why? regex are greedy - the second pattern ".*" matches up to the end of line, because the third pattern "[[:space:]]*" can be 0 characters long.

Better use this:
Code:
jan@jack:~> VAR1=`echo "$VAR" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'`
jan@jack:~> echo "[$VAR] [$VAR1]"
[  123  456    ] [123  456]
If your variables can contain spaces, you should quote them always - otherwise you see an output as in your script - trailing and leading spaces are gone away and spaces in the middle are shortened to one.

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
Error when starting up snort: bash:!/bin/sh/usr/local/bin/snort :Eent not found cynthia_thomas Linux - Software 1 11-11-2005 02:59 PM
bash: /usr/bin/localedef: No such file or directory satimis Linux From Scratch 2 07-28-2005 01:53 AM
path in services wrong for clamav updated frm 0.75 to 0.80 usr/bin vs usr/local/bin Emmanuel_uk Linux - Newbie 3 04-22-2005 01:02 AM
.CGI scripts enabled with #!/usr/bin/perl -i?? computerdaves Fedora 8 02-18-2005 04:03 PM
bin/bash:usr/bin/lpr NO SUCH FILE OR DIRECTORY Adibe_Hamm Linux - Newbie 3 10-14-2003 02:30 AM

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

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