Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 4-Mar-17 18:04pm
Comments
[no name] 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.
Patrice T 4-Mar-17 18:20pm    
What is the language ?

1 solution

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[^]
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900