Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-11-2008, 10:53 AM   #1
LQ Newbie
Registered: May 2006
Location: Dunfermline, Scotland
Distribution: Ubuntu 8.04 LTS
Posts: 10

Rep: Reputation: 0
Question Using ASCII character codes in a shell script (Linux)

Hi all,

After using the command "du -s $dir" to put the total size of a directory into a variable I want to strip the directory size out of the variable and loose the accompanying directory path.

To do this I've been trying to compare each character until I find the space between directory size and path and then ignore everything after it

When I compare the individual characters of a variable with " " (space) I find that any time a 0 (zero) appears it matches the " " (space). I think I should be able to use the ASCII character code for space, chr(32) to do the comparison but I don't know the syntax to allow this.

The command I'm using is simply: -

if [ "${char1}" -eq "${test}" ] ; then

where ${char1} is the first character of the variable and ${test} is the character against which it is compared.

Can anyone tell me how to do this?

Old 06-11-2008, 11:45 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In bash there are different ways to manage ASCII codes. The echo command with option -e recognizes octal sequences, for example
echo -e "\0127"
prints out the character W. You can also assign a character to a variable given its octal code, for example
will assign the character W to the variable var, or you can use this syntax directly in comparisons.

Anyway, for your specific problem you can simply do something like
du -s $dir | cut -d\t -f1
du -s $dir | awk '{print $1}'


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple character replacement by shell script mauran Programming 14 07-13-2007 02:46 AM
Editing Ascii File Using Shell Script rohit2983 Programming 7 01-03-2007 06:27 AM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
Escape character not working in shell script philipz Programming 1 04-29-2004 09:58 AM
ASCII Codes in linux slackmagic Linux - Newbie 2 12-22-2003 09:25 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration