The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple Scripting Problem felixwhoals Shell Programming and Scripting 3 12-16-2007 01:24 PM
Simple Array in Ksh Scripting ravikirankethe UNIX for Dummies Questions & Answers 2 11-01-2006 11:35 AM
HELP me PLS... Simple Scripting! liezer Shell Programming and Scripting 2 09-14-2006 10:50 AM
simple scripting question gennaro Shell Programming and Scripting 3 06-17-2005 03:49 AM
any tutorials on simple scripting? xyyz UNIX for Dummies Questions & Answers 3 03-13-2003 01:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2005
Nads Nads is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 6
Help with simple scripting actions

Hi,

I am a beginner in unix shell scripting.

I wanted simple information like
1- How to know what are the number of command line options given for the script file?

2- How to check if a variable value is interger or string?

3- How to use awk to replace value of a variable
For example I have a file with 5 lines
1
2 ResourceID=ms1
3
4 ResourceID=ms2
5

How can I replace ms1 and ms2 values with other values using awk?

Any site that can give me this information will also be very helpfull.
Thanks!
  #2 (permalink)  
Old 04-27-2005
RiMKa RiMKa is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 5
Quote:
Originally Posted by Nads
Hi,

I am a beginner in unix shell scripting.

I wanted simple information like
1- How to know what are the number of command line options given for the script file?

2- How to check if a variable value is interger or string?

3- How to use awk to replace value of a variable
For example I have a file with 5 lines
1
2 ResourceID=ms1
3
4 ResourceID=ms2
5

How can I replace ms1 and ms2 values with other values using awk?

Any site that can give me this information will also be very helpfull.
Thanks!
Hi,

regarding Q1:
if your shell supports getopts - I suggest you have a look at it (man getops). Otherwise you will need to write a small shell parsing code. (use $# shell variable that reports the amount of arguments passed to shell script).

regarding Q2:
I would use the following shell code:

expr ${VALUE} + 1 2>/dev/null
if [ $? -eq 0 ]
then
echo "Integer value"
else
echo "String value"
fi


cheers,
Slava R.
  #3 (permalink)  
Old 04-27-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Regarding Q2, this depends on which shell you are using. It also depends on what an integer is. If 123 +456 -789 all count as integers, and if we are using ksh, I am fond of:

Code:
#! /usr/bin/ksh
while (($#)) ; do
        if [[ $1 = ?(+|-)+([0-9]) ]] ; then
                   echo $1 is an integer
        fi
        shift
done
exit 0

  #4 (permalink)  
Old 04-27-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
for Q3. replacing with "hello"


Code:
 sed 's/=.*$/=hello/g' file1

  #5 (permalink)  
Old 04-28-2005
Nads Nads is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 6
Thanks!!

Regarding Q3: I meant to replace values ms1 and ms2 with ms3 and ms4.
Using sed or awk, I can assign same value to ResourceID. I couldnt figure out how I can assign different values.
  #6 (permalink)  
Old 04-29-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Maybe try...
Code:
sed -e s/=ms1/=ms3/ -e s/=ms2/=ms4/ file1 > file2

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0