SliTaz SliTaz Forum

You are not logged in.

#1 2013-02-08 10:21:26

arodulfo
Member
Registered: 2012-11-03
Posts: 71

Implementing substrings in shell commands

Dear all,

I am doing some scripting works in our [c]SliTaz 4[/c] platform.

Managing the results of some of our efforts, I wanted to use part of a bigger keyword as the name for a folder I need to create.

I know there is [c]expr substr n m[/c] as the shell way to make such partial use of keywords. However, your implementation of the bourne shell has no provision for [c]substr[/c]. Is there any way to overcome this lack of provision?

kind regards,

Antonio

Offline

#2 2013-02-08 11:00:39

lexeii
Administrator
Registered: 2012-03-21
Posts: 1,853

Re: Implementing substrings in shell commands

Hi arodulfo,

I found BASH examples in the google:

BASH example 1:

[c]stringZ=abcABC123ABCabc
#       123456789......
#       index starts from 1

echo $(expr substr $stringZ 1 2)              # ab
echo $(expr substr $stringZ 4 3)              # ABC[/c]
BASH example 2:

[c]stringZ=abcABC123ABCabc
#       0123456789.....
#       index starts from 0

echo ${stringZ:0}                            # abcABC123ABCabc
echo ${stringZ:1}                            # bcABC123ABCabc
echo ${stringZ:7}                            # 23ABCabc

echo ${stringZ:7:3}                          # 23A
                                             # return 3 symbols[/c]
SliTaz is small distro and not included BASH by default (but you can install it: [c]# tazpkg -gi bash[/c]), SliTaz use Busybox's ash (manpage ash). And better of all, second BASH example works on Ash!

Good luck in your scripting!

Offline

#3 2013-02-08 11:02:27

Trixar_za
Administrator
Registered: 2011-03-29
Posts: 1,506

Re: Implementing substrings in shell commands

Actually expr is part of the coreutils and not the shell, so installing the coreutils-conditions package should solve this problem for you by providing the real expr rather than the busybox version.

Offline

#4 2013-02-08 11:15:27

arodulfo
Member
Registered: 2012-11-03
Posts: 71

Re: Implementing substrings in shell commands

@Aleksej, @Trixar_za:

Provided I can solve it with no additional installations, I would opt for Aleksej's hint.

I don't really want to get too far from initial SliTaz 4 config.

I'll try your hints and report my results back here ASAP.

Kind regards,

Antonio

Offline

#5 2013-02-08 11:26:00

arodulfo
Member
Registered: 2012-11-03
Posts: 71

Re: Implementing substrings in shell commands

@Aleksej: Brilliant! I never stop learning! It works as expected! Thank you so much. I needed just that!

Kind regards,

Antonio

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.017 seconds, 7 queries executed - Memory usage: 1.54 MiB (Peak: 1.77 MiB) ]