Help with simple scripting actions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple scripting actions
# 1  
Old 04-27-2005
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  
Old 04-27-2005
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  
Old 04-27-2005
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  
Old 04-27-2005
for Q3. replacing with "hello"

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

# 5  
Old 04-28-2005
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  
Old 04-29-2005
Maybe try...
Code:
sed -e s/=ms1/=ms3/ -e s/=ms2/=ms4/ file1 > file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Simple Shell Scripting

1. The problem statement, all variables and given/known data: An argument example: ../path/cse/lab3/remove Right now, it's printing out all the directory and files in 'lab3'. I want it to print out all the files in 'remove'. I'm not sure how to do that. (I want to use a for loop) 2.... (2 Replies)
Discussion started by: spider-man
2 Replies

2. Shell Programming and Scripting

Help :: Simple Shell Scripting

Hello, I want to find the "IP-OF-SERVER" in /etc/squid/squid.conf And replace it with The IP of server. I know this command returns the IP of server : ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' And I can replace with sed. : sed -i... (4 Replies)
Discussion started by: Ghadamyari
4 Replies

3. Shell Programming and Scripting

Simple scripting.

echo "what is your username?" read username echo $username echo /home/$username $backup="backup" $restore="restore" # # if then echo "No username provided" else echo "hi $username would you like to backup or restore?" read userrequest echo $userrequest if then ... (4 Replies)
Discussion started by: EwanD
4 Replies

4. Shell Programming and Scripting

help with scripting a simple menu

Hi there. I'm trying to teach myself UNIX but the book I bought is a bit confusing. I'm trying out this exercise and I think I'm on the right track, but I'd appreciate any suggestions on how to improve what I have so far. Also, I'm not clear on how to use the read command to utilize the user's... (3 Replies)
Discussion started by: Jsmith
3 Replies

5. Shell Programming and Scripting

help with simple korn scripting

Hi, The logic is very simple but I can't seem to make this work in Korn shell. I need to check two files to make sure there is no errors. Each of the file will have number. For example, first file btt.txt will have 112 which is good. Second file bgg.txt will have 6 which is also good. If I... (4 Replies)
Discussion started by: samnyc
4 Replies

6. Shell Programming and Scripting

Simple Scripting Problem

Hi there, I was trying to add a line of text in the middle line of a file. I have counted the lines in the file, and then I divide it into 2, after that I am stuck on how am I suppose to append the line on that file? When I tried to use this command 'second line >> filename' it appends it at... (3 Replies)
Discussion started by: felixwhoals
3 Replies

7. UNIX for Dummies Questions & Answers

Simple Array in Ksh Scripting

Ksh Scripting Can some one give me a simple example of array operations using ksh. For Ex: week_array = {Sunday Monday Tuesday Wednesday Thursday Friday Saturday} I want to assign and retrieve and print them along with their index. I am looking for the o/p like: 0 Sunday 1 Monday ... (2 Replies)
Discussion started by: ravikirankethe
2 Replies

8. Shell Programming and Scripting

HELP me PLS... Simple Scripting!

this is my script.... SQL> select * from dba_profiles 2 where resource_name in ('FAILED_LOGIN_ATTEMPTS','PASSWORD_LOCK_TIME') 3 order by profile; and this is the output... PROFILE RESOURCE_NAME RESOURCE... (2 Replies)
Discussion started by: liezer
2 Replies

9. Shell Programming and Scripting

simple scripting question

I am new to scropting and I am just trying to work on a few simple things.... Using sh I want to do something similar to the follwoing run a simple command like a dig then I want it to get the server that it is authorotative and return it to me saying somehting like xx.xx is authorotative. I am... (3 Replies)
Discussion started by: gennaro
3 Replies

10. UNIX for Dummies Questions & Answers

any tutorials on simple scripting?

i'm not looking for anything that deals with "if-then" scripts. i'd like something simple on how to run a series of processes. for example the following: 1. ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/tar_files/ 2. lcd / 3. get pkgsrc.tar.gz 4. bye 5. cd /usr 6. rm -rf pkgsrc 7. cd... (3 Replies)
Discussion started by: xyyz
3 Replies
Login or Register to Ask a Question

Featured Tech Videos