ksh on HP-UX -- variable expansion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ksh on HP-UX -- variable expansion
# 1  
Old 02-06-2005
ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example:

Code:
script -s hello -t goodbye '*.d*'

In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to attempt to expand it. I want the string *.d* to get passed to the script.

The problem:

If there is a filename that matches the pattern of the final argument, that filename(s) get passed onto the script, instead of *.d*
How do I make sure the string I put in the command line gets passed on, no matter what is in the current directory?
# 2  
Old 02-07-2005
Inside the script, you should be ok if you always have double quotes around that argument. But you can turn off wildcard expansion with:
set -o noglob
# 3  
Old 02-07-2005
This is an annoyingly complicated script. I can't turn off wildcard expansion because there may be wildcards in another parameter. I put some echo statements into the script and found that the *.d* gets passed into the script literally with no problem. The script then passes the arguments into a function like so:

fa $*

That function then expands the wildcard (which I dont want it to do). If I put double quotes around $* as such:

fa "$*"

It gets passed into the function unexpanded (*.d*). Great!

But the tricky part is then I call getopts within function fa, which treats all parameters as one because of the double quotes. So I'm back at square one Smilie
# 4  
Old 02-07-2005
Turn off globbing.
Set the variables.
Turn on globbing
Expand the variable that needs expanding.

Code:
$
$ set -o noglob
$ x=f*
$ y=f*
$ echo "x = $x             y=$y"
x = f*             y=f*
$ set +o noglob
$ echo "x = $x             y=$y"
x = f*             y=f*
$ x=$(echo $x)
$ echo "x = $x             y=$y"
x = faqs fileperms first.file form funky             y=f*
$

# 5  
Old 02-08-2005
Excellent. It worked. Thanks for the explanation.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1 I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )echo $SOME_LINE... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Shell variable expansion in awk

I want to split one file input.tab into two separate ones, odd lines to input_reads1.txt, even lines to input_reads2.txt for a serial of files with similar name pattern. Also I want to "match" input/output file names to keep consistency of file name: CSEL_02.0_input.tab CSEL_03.4_input.tab... (2 Replies)
Discussion started by: yifangt
2 Replies

3. Shell Programming and Scripting

Protecting variable indicator ($) from expansion

Hello, I use a lot this command to edit a bunch of files at once find . -name filename" | xargs -ifoo sh -c 'echo foo ; sed "s/pattern1/pattern2/" foo > ./tmp ; mv -f ./tmp foo' I'm trying to put a function on my .bashrc file. function loopSed() { local filename=$1 local... (2 Replies)
Discussion started by: phollox
2 Replies

4. UNIX for Dummies Questions & Answers

Can I use a variable with brace expansion?

So, I was bored on the train today, and was thinking of ways to loop through elements of an array. I came up with the following simple script, but it doesn't work as brace expansion doesn't seem to work with variables. Is there something I'm missing, or does the shell just not work like this? ... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

5. Shell Programming and Scripting

delay variable expansion

Hi forum, in my bash script I've many lines executing commands with redirection to log files. ... xyz_cmd 2>&1 > $BASENAME.$LINENO The trailing part of these lines doesn't look nice and I like to put it into a variable. The (not working) idea is something like that ... (3 Replies)
Discussion started by: wolfi089
3 Replies

6. Shell Programming and Scripting

Variable expansion in sed

The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be... (4 Replies)
Discussion started by: tiggyboo
4 Replies

7. Shell Programming and Scripting

Bash variable delayed expansion?

i write a batch file , here is the content. dirname='date +%Y-%m-%d' mkdir dirname but it doen's work, it just create a folder named date and +%Y-%m-%d. i have tried run the command seperately in the bash prompt. after the first sentence executed , i use $dirname to watch the value of... (4 Replies)
Discussion started by: premotheus
4 Replies

8. UNIX for Dummies Questions & Answers

Variable brace expansion

I'm in the habit of using the following type of loop structure: for num in `seq $1 $2` do command doneWhile `seq $1 $2` is not exactly a huge resource hog, I would like to learn a better way. It seems that brace expansion is a good way to go: for num in {3..10}The problem, though, is... (2 Replies)
Discussion started by: treesloth
2 Replies

9. Shell Programming and Scripting

bash - delay expansion of variable

Hello - I have a bash script which does some logging, and I'd like to include the line number of the echo statement that pipes into $LOGGER: MYPID=$$ MYNAME=`basename $0` LOGGER="/usr/bin/logger -t $MYNAME($LINENO) -p daemon.error" ... echo 'this is an entry into the log file' | $LOGGER ... (3 Replies)
Discussion started by: scandora
3 Replies

10. Shell Programming and Scripting

Basic variable expansion not working...

#!/usr/bin/bash if then echo "Not valid arguments entered. Just username should be entered." else USER_NAME=$1 FILE_NAME=$USER_NAME.info UNN=STUDIN\\\\$1 echo $UNN last STUDIN\\\\$1 last UNN If I type `last STUDIN\\eip060` it works but if I try to expand it with variable it is... (5 Replies)
Discussion started by: Zammy_bg
5 Replies
Login or Register to Ask a Question