please explain why this ksh echo deletes character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting please explain why this ksh echo deletes character
# 1  
Old 01-31-2012
please explain why this ksh echo deletes character

Please explain why this works. Am unable to find another definition for the '%', which would explain this behaviour:

Code:
spaceLeft=`df -h /myPartition | tail -1`
# output looks like:   /dev/sda5              10G  1.2G   12G  29% /

set -- $space

#this deletes the trailing '%' sign, which is what is needed, but wondering how?
# can someone better explain this statement for me :-)
$(echo ${5%\%})

# prints 29


Last edited by methyl; 01-31-2012 at 01:57 PM.. Reason: please user code tags
# 2  
Old 01-31-2012
Quote:
set -- $space
Should probably be:
set -- $spaceLeft
The set command parse's the contents of $spaceLeft and places each component into $1 $2 $3 $4 $5 respectively.

Quote:
$(echo ${5%\%})
The 5 is referring to $5. The command is checking the last character of the contents of $5 for a % sign (protected as \%) and deleting that character.

It is explained in the "man" page for your Shell in the section about:
Quote:
${parameter%pattern}
Notice that the first percentage sign % is part of the syntax of the command which is why the percentage sign we are looking for is escaped as \% .
# 3  
Old 01-31-2012
It's a string operation:

${string%substring} = Strip shortest match of $substring from back of $string

In your case the backslash takes away the special meaning of the percent sign, so it says strip off the percent sign.

Actually your code:
Code:
$(echo ${5%\%})

will try to run a command called "29". I think you don't want the $( ) syntax around your echo statement?
# 4  
Old 01-31-2012
I think O/P meant something like:
Code:
percentage=$(echo ${5%\%})
echo "${percentage}"

# 5  
Old 01-31-2012
Save a process, no need to run echo (unless there is more to the story that has been edited out for example purposes)
Code:
percentage=${5%\%}

These 2 Users Gave Thanks to gary_w For This Post:
# 6  
Old 01-31-2012
Thank you, question answered. I was not aware of the variable substitution offered by ${var%pattern}
# 7  
Old 01-31-2012
In "man ksh" it's in the section headed "Parameter Substitution".
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo to file using SH without adding newline character

Hello! I am able to do this in bash, using: echo -ne HELLO > file.txt and then, 'HELLO' is written into file.txt without the newline character to be added in the end of the file. How is this possible to be done using sh instead of bash? If I try something similar is SH, then inside... (3 Replies)
Discussion started by: hakermania
3 Replies

2. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

3. Shell Programming and Scripting

\n in ksh using echo & printf

#!/usr/bin/ksh var1="Hi World" var2="Morning" var3=$(echo "$var1" \n "$var2") echo $var3 var3=$(printf "$var1 \n $var2") echo $var3 Output Any way to get in my $var3 ? (7 Replies)
Discussion started by: dahlia84
7 Replies

4. UNIX for Advanced & Expert Users

echo in ksh sh & bash

Hello, I have lib file which contain a function that get text to print on screen by echo command. Several scripts are inculde this lib and use this function. Each one of them is written in different shell language (sh ksh & bash). This causing some issues when using backslash charater as... (4 Replies)
Discussion started by: Alalush
4 Replies

5. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

6. UNIX for Dummies Questions & Answers

Weird character in between echo function

Hi All, Appreciate if anyone can help. I've a script where it does echo function like this while do FILE_ARG="cu0${w}_${FILE}_${DT}.av" ORACLE_ERROR=`grep "ORA-" ${FILE_ARG}` if ]; then Func_Log_Writer "Fail! ${FILE_ARG}\n" Func_Log_Writer "Error message:... (2 Replies)
Discussion started by: agathaeleanor
2 Replies

7. Shell Programming and Scripting

Reading password and echo * character

Hi, First of all i am using solaris 10. I want to write a script that ask user to enter password and read the character input from keyboard. The ask to re-enter the password and then if they are match it will accept. But my problem is I want to echo a '*' character instead of the character I... (4 Replies)
Discussion started by: alanpachuau
4 Replies

8. Shell Programming and Scripting

ksh - how to echo something in color and bold

Hi all, I was to echo Hi in Red and Bold ; and echo There is in Green and bold I got bold to working using tput bold but i am having hard time getting the color. Any help is appreciated, jak (4 Replies)
Discussion started by: jakSun8
4 Replies

9. UNIX for Advanced & Expert Users

Echo with ksh shell

Hi All, I'm trying to use the "echo" command in a korn shell script, and I want it to drop the trailing newline. Now I know that with the bash shell, the "-n" flag would solve this issue. Does anyone know how this can be done with the korn shell? Cheers Khoom (10 Replies)
Discussion started by: Khoomfire
10 Replies

10. Shell Programming and Scripting

what is ksh equivalent of bash echo -n ?

Hi folks, I need to stop printing a new line after echoing a string in KSH. i know bash provides echo -n "string" what is the ksh equivalent for this ? (3 Replies)
Discussion started by: mudhireddy
3 Replies
Login or Register to Ask a Question