Parameter problem in my script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameter problem in my script
# 8  
Old 06-01-2008
Bourne shell does not have ${var%pattern}. If you think you're using bourne shell, you're probably on Linux (sh points to bash).

Can you point to the docs saying that the pattern is actually a regexp? I doubt it... and I can't find it in the man pages.
# 9  
Old 06-01-2008
FreeBSD sh , and I don't have bash.
# 10  
Old 06-02-2008
MySQL

Thank you all for your help Smilie
# 11  
Old 06-02-2008
Quote:
Originally Posted by danmero
I use Bourne shell(/vin/sh)sh

Yes, however you can use regexp.
Code:
y=${x%.*.?}

On Solaris, using /bin/sh (definitely a good old-fashioned Bourne shell):

Code:
#!/bin/sh
x="hellothere"
y=${x%.*.?}
echo "x is: %x, y is: $y"

Gives:
bad substitution

Simplifying, by changing the ${x%.*.?} to ${x%there} gives the same response.

If I use ksh:
Code:
#!/bin/ksh
x="hellothere"
y=${x%there}
echo "x is: %x, y is: $y"

I properly get: x is: hellothere, y is: hello. Bash works, as well. Looks like it's pattern matching only.

But assuming that regular expressions are supported, I'll test
Code:
#!/bin/sh
x="hellothere"
y=${x%..*}
echo "x is: %x, y is: $y"

...This should match "1 of anything, followed by 0 or more instances of anything." That is, y should be completely empty. Testing with sh, ksh, and bash:
  • sh gives "bad substitution"
  • ksh gives "x is: hellothere, y is: hellothere"
  • bash gives the same
...So it doesn't work. If I assume the variable substitution is a pattern instead, that is I assume y=${x%..*} means "y equals x but remove a literal dot followed by a literal dot followed by anything (or nothing)", then let me change x to be something that such a pattern should match:
Code:
x="front..hellothere"

In this case, y should be "front". The results are:
  • sh I won't even try
  • ksh gives "x is: front..hellothere, y is: front"
  • bash gives "x is: front..hellothere, y is: front"
...That is, both ksh and bash work properly.


This shows that for Bourne shell, ksh88, and bash, this variable construct uses pattern matching, not regular expressions.

See Regular Expressions - a Simple User Guide for more information about regular expressions. See also The Regex Coach - interactive regular expressions for a very useful (Windows-based) tool.
# 12  
Old 06-02-2008
Reading Material

There's a great explanation and examples of Korn shell variable expansion here.

InformIT: Variables and Parameters in the Korn Shell > Variable Expansion
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

2. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

3. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

4. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

5. Programming

Problem with STL's std::set container parameter to the operator << ()

Hi, I have this following code which gives me error when compiling. The problem is happening at the point where I create a const_iterator inside the overloaded insertion operator (i.e) operator << () function. The template argument version of set is not correct I guess. Could anyone please pitch... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. AIX

Audit problem : A system call received a parameter that is not valid.

while i try to start the audit i have the below error message . audit>audit start ** auditing enabled already A system call received a parameter that is not valid. please advice (6 Replies)
Discussion started by: thecobra151
6 Replies

7. Shell Programming and Scripting

parameter identification problem

Hello everyone suppose i have a script which can take any no. of parameters and in any order then how can i identify a particular parameter at which position it is entered suppose i call a script with four parameter ./abc.sh a b c d in above calling a is called at no. 1... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

8. Shell Programming and Scripting

Parameter Passing problem

Hi All, I developed a KSH script which will accept two parameters as input. These two parameters are some directories paths. In the script i am validating the number of paramaters it received as below #-------------------------------------- # Check Command line arguments... (8 Replies)
Discussion started by: Raamc
8 Replies

9. Shell Programming and Scripting

Problem if parameter has space in it using loop

for cmdopts in $*;do case $cmdopts in -mode) mode="$2";shift 2 ;; -server) server="$2";shift 2 ;; -Id) Id="$2";shift 2 ;; -passwd) passwd="$2";shift 2 ;; -rmtDir) rmtDir="$2";shift 2 ;; -lcDir) ... (9 Replies)
Discussion started by: pinnacle
9 Replies

10. Shell Programming and Scripting

Parameter Problem With an Array Variable

Hi, I have two bash shell scripts: test: rrdhcp78-120:test_data msb65$ cat ~/bin/test #!/bin/sh array1=() echo 'elements in array1:' ${#array1} ~/bin/test2 $array1 test2: rrdhcp78-120:test_data msb65$ cat ~/bin/test2 #!/bin/sh array2=${1} (5 Replies)
Discussion started by: msb65
5 Replies
Login or Register to Ask a Question