Click here to Skip to main content
13,258,269 members (49,540 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I'am trying to create a function that takes name as argument(<username>_<num> and gives R +1 as result.( in linux)
e.g newfunc apple_1
apple_2

What I have tried:

function newfunc {
	INPUT=$1;
	NUMBER=$(echo INPUT | tr -dc '0-9');
	TEXT=$(echo INPUT | tr -dc '[:alpha:]');
	NUMBER=$((NUMBER+1));
	echo"${USERNAME}_${NUMBER}"
}
newfunc
Posted 4-Mar-17 11:46am
Updated 4-Mar-17 19:04pm
Comments
NotPolitcallyCorrect 4-Mar-17 17:36pm
   
You might have better luck tagging your question with the actual programming language this is. Linux is an operating system not a programming language.
ppolymorphe 4-Mar-17 18:20pm
   
What is the language ?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is a Unix/linux shell script, there are a couple of syntax and logic errors, check this out:
newfunc() {
   	INPUT=$1
   	echo $INPUT
	NUMBER=$(echo "${INPUT:$((${#INPUT}-1)):1}")
	echo ${NUMBER}
	TEXT=$(echo "${INPUT:$i:-1}")
	echo $TEXT
	NUMBER=$((NUMBER+1))
	echo "$TEXT${NUMBER}"
}
 
newfunc "apple_1"
The ouputs:
apple_1
1
apple_
apple_2

Learn Unix Shell Functions[^]
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 5 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100