Can't stop shell interpreting special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't stop shell interpreting special characters
# 1  
Old 11-23-2005
Can't stop shell interpreting special characters

I am struggling with the following sample code:

Code:
array1=(a b c d)
array2=(* * * *)

print ${array1[2]}
print ${array2[2]}

It returns 'c' and the name of a file in the directory I'm in.

I can't for the life of me work out how to prevent the shell interpreting the '*' and just get it to return the plain character ... is there a way to do this?


Also, my Learning the Korn Shell book lists several sed-like functions you can use to edit variable names, e.g.

Code:
variable=string
variable=${variable/g/gs}
print $variable

returns 'strings'. But Sed has a function where you can 'store' part of the pattern matched, and get it back in the replacement string, e.g.

Code:
print $variable | sed "s/\(strin\)g/\1/"

returns 'strin'. Does the Korn shell have a similar feature that's not in my book?
# 2  
Old 11-23-2005
Q1: Turn globbing off (set -f) before assigning * to an array element; turn it on again after you are done (set +f)
Code:
set -f
set -A array * * * *
set +f

Q2: Maybe I don't understand what you want to do but variables in Korn shell can be manipulated by using sed so, at a minumum, you can do something like this:

Code:
variable=$(print $variable | sed "s/\(strin\)g/\1/")

# 3  
Old 11-23-2005
That sed-like variable manipulation arrived in ksh95. You are probably using ksh88.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New to Shell Scripting: Need help interpreting example function

In this example function below, I cannot figure out what certain parts mean. if ! echo $PATHwhat is "if !"? (^|:)$1($|:) What is ^|: and$|:? pathmunge () { if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then if ; then PATH=$PATH:$1 else... (3 Replies)
Discussion started by: MemberName
3 Replies

2. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

3. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

6. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

7. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

8. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

9. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

10. Shell Programming and Scripting

Bourne Shell: Special characters Input Prevention

Environment: Sun UNIX Language: Bourne Shell Problem: I am writing a script that allows user to key in a directory. Example: /root/tmp. Since the user can key in anything he/she wants, I need to validate to make sure that he/she does not key in anything funny i.e.... (37 Replies)
Discussion started by: totziens
37 Replies
Login or Register to Ask a Question