ease bash/sh coding: Has someone right(,), left(,), and len(,) scripts for /usr/bin ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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."
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:
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
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
thx to Hko for the hint to the shorter right command.
You're welcome :-)
FYI: The space between ':' and '-' is needed.
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
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.
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:
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 !!
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:
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.