How to handle variable with spaces in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to handle variable with spaces in ksh
# 1  
Old 04-11-2012
How to handle variable with spaces in ksh

I'm having issue capturing a value from file.list with a multiple spaces in a variable $i, tried various options like using double quotes, no quotes, single quotes, curly braces but to no avail.

Code:
cat file.list
aaa test bbb
ccc test ddd
eee test fff

for i in `cat file.list`
do 
echo "$i"; ls -ld "$i"
done

Below is the output I get while running the above script:
aaa not found
test
test not found
bbb
ccc
bbb
ccc not found
test
test not found
ddd
eee
ddd
eee not found
test
test not found
fff
fff not found

# 2  
Old 04-11-2012
This is a side-effect of useless use of backticks. The shell splits where it pleases, not on lines.

To read lines, use the read command.

Code:
while read LINE
do
        echo "Line was $LINE"
done <inputfile

# 3  
Old 04-11-2012
How to handle variable with spaces in ksh

Your answer worked for me, thank you for prompt response and ofcourse backtick is not worth using...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to handle variable with special character?

Hi Gurus, I have a requirement which needs to pass a parameter when calling the script, using this parameter to find a file name stored in master file. then read the file content. the issue is in the file name has a special character "$". don't know how to handle this. below is example: the... (10 Replies)
Discussion started by: green_k
10 Replies

2. Shell Programming and Scripting

How to handle grepping variable data containing wildcards?

I have a lot of files with keywords and unique names. I'm using a shell script to refer to a simple pattern file with comma separated values in order to match on certain keywords. The problem is that I don't understand how to handle the wildcard values when I want to skip over the unique names. ... (5 Replies)
Discussion started by: abercrom
5 Replies

3. Shell Programming and Scripting

ksh: removing all white spaces

'String' file contains the following contents, D11, D31, D92, D29, D24, using ksh, I want to remove all white spaces between characters no matter how long the string is. Would you please give me some help? (1 Reply)
Discussion started by: yoonius
1 Replies

4. Shell Programming and Scripting

array in ksh with elems containing spaces

Hi all, I've a .csv file containing data per line delimited with '|' (The fields may contains elements with spaces). e.g. (really a sample) ID|Name|fon 12345|Celal Dikici|+4921123456 12346|Celal Dikici Jun.|+4921123456 12347|Celal Dikici Sen.|+4921123456 12348|Celal|+4921123456... (3 Replies)
Discussion started by: Celald
3 Replies

5. Shell Programming and Scripting

ksh - read file with leading spaces

Hi, Could someone has any suggestions on this? When read a line from a file, I need to check the first char in the line, it could be a space or any char. But the leading spaces are removed by read. Thanks. (2 Replies)
Discussion started by: momi
2 Replies

6. Shell Programming and Scripting

ksh: Comparing strings that contain spaces and working with substrings

Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks. Here are two such examples. I want to save the first 19 characters of the following string to a variable. "Operation Completed and blah blah blah" I know this works (from another thread): ... (2 Replies)
Discussion started by: nancylt723
2 Replies

7. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

8. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

9. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question