Please help me out with this script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help me out with this script
# 1  
Old 08-14-2011
Please help me out with this script

Hi,

Script gurus please help me out with this script.
when i am running the script in vi i am getting the below error

Running the script in vi


Code:
#!/bin/bash
for (( k=0;k<2;k++ ))------------>form here loop will run two times
do
echo -n "enter the director name : "--->entering the director name which 10c here
read NAME
echo -n "enter the Port number: " ------>entering port number 1
read NUM
for i in `cat /tmp/p1`
do
echo "map dev" $i "to dir "
echo $NAME":"` echo $NUM`
for j in `cat /tmp/p2`
do
echo "target=0","lun="$j";"
done
done
done
 
: error 1 /tmp/devices: syntax error at line 4: `(' unexpected

These two are my inputs
Code:
bash-3.00# cat /tmp/p1
1133
113B
1143
114B
bash-3.00# cat /tmp/p2
000E
000F
0010
0011

But when I am, running the script in this its working but not giving the require o/p
Code:
for (( k=0;k<2;k++ )); do echo -n "enter the director name : "; read NAME; echo -n "ent
er the Port number: "; read NUM; for i in `cat /tmp/p1`; do echo "map dev" $i "to dir ";echo $NAME":"` echo $NUM`; for j in `cat /tmp/p2`; do echo "target=0","lun="$j";";done; done; done

Output
Code:
enter the director name : 10c
enter the Port number: 1
map dev 1133 to dir
10c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 113B to dir
10c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 1143 to dir
10c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 114B to dir
10c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
enter the director name : 11c
enter the Port number: 1
map dev 1133 to dir
11c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 113B to dir
11c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 1143 to dir
11c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;
map dev 114B to dir
11c:1
target=0,lun=000E;
target=0,lun=000F;
target=0,lun=0010;
target=0,lun=0011;

But I want to be my output like this in the same line and for /tmp/p2 it will run only one time.

Code:
enter the director name : 10c
enter the Port number: 1
 
map dev 1133 to dir 10c:1 target=0,lun=000E;
map dev 113B to dir 10c:1 target=0,lun=000F;
map dev 1143 to dir 10c:1 target=0,lun=0010;
map dev 114B to dir 10c:1 target=0,lun=0011;
 
enter the director name : 11c
enter the Port number: 1
 
 
map dev 1133 to dir 11c:1 target=0,lun=000E;
map dev 113B to dir 11c:1 target=0,lun=000F;
map dev 1143 to dir 11c:1 target=0,lun=0010;
map dev 114B to dir 11c:1 target=0,lun=0011;

Thanks in advance

Regards
Nirjhar

Last edited by zxmaus; 08-14-2011 at 11:58 PM.. Reason: added code tags
# 2  
Old 08-14-2011
I'm confused by what it is you mean when you say "trying to run from vi." Using the :! command vi command? Unfortunately I cannot explain the error you are getting; your script runs without error for me both under Kshell and bash.

Your undesired output is a result of your logic. It appears based on your p1 and p2 files (thanks for adding their contents) that you want line 1 from each file to be matched and used, then line 2 from each, etc. The nested loops in your script will take line 1 from the p1 file and match it with all lines in p2, then take line 2 from p1 and match it with all lines from p2 and so forth. Giving you way more output sets than it seems you desire.

The other problem is that you are using echo to print partial lines and that will always insert a new line. You can use an option to echo to suppress the newline, but that's not always portable, so I recommend you use printf instead. In this case, after the script is corrected, you only need one printf anyway, so you could use echo.

Here is how I would write the script:
Code:
#!/usr/bin/env bash
typeset -a p1v      # arrays to hold data from each p file
typeset -a p2v

i=0
while read v        # load data from p1
do
    p1v[$i]="$v"
    i=$(( $i + 1 ))
done </tmp/p1

i=0
while read v        # load data from p2
do
    p2v[$i]="$v"
    i=$(( $i + 1 ))
done </tmp/p2

for (( k=0;k<2;k++ ))
do
    echo -n "enter the director name : "
    read NAME
    echo -n "enter the Port number: "
    read NUM

    for (( j=0; j<$i; j++ ))            # for each row seen in p2, print a line
    do
            printf "map dev ${p1v[$j]} to dir $NAME:$NUM target=0,lun=${p2v[$j]};\n"
    done
done

exit

This User Gave Thanks to agama For This Post:
# 3  
Old 08-14-2011
Please help me out with this script

Hi,

Thanks for your great help.

1.I am putting all my code in the file by using vi editor and getting "syntax related error"
2.I am new to shell scripting you have made this code by using array,
If you have time would you please explain the body of your code.
what is typeset in this code. while compiling this script i am getting below error.

Code:
bash-3.00# sh  /tmp/t1
/tmp/t1: typeset: not found
/tmp/t1: typeset: not found
/tmp/t1: syntax error at line 9: `i=$' unexpected

Regards
Nirjhar

Last edited by zxmaus; 08-14-2011 at 11:59 PM.. Reason: added code tags
# 4  
Old 08-14-2011
Quote:
Originally Posted by nirjhar17
Hi,

Thanks for your great help.

1.I am putting all my code in the file by using vi editor and getting "syntax related error"
Ok, so you're getting the error after you save the file and when you try to execute it? That makes a bit more sense.

Quote:
2.I am new to shell scripting you have made this code by using array,
If you have time would you please explain the body of your code.
what is typeset in this code. while compiling this script i am getting below error.

bash-3.00# sh /tmp/t1
/tmp/t1: typeset: not found
/tmp/t1: typeset: not found
/tmp/t1: syntax error at line 9: `i=$' unexpected
The typeset -a name defines the variable name as an indexed array. You're getting the typeset errors because you are using the bourne shell (sh) instead of bash or ksh. If you enter the command bash /tmp/t1 you shouldn't get the errors.

Further, if you make your script executable, and have the proper #! statement as the first line, you will only need to type the filename of the script on the command line.

Code:
chmod 755 /tmp/t1    # make the script executable
/tmp/t1                # execute the script

The very first line in your script file (no blank lines before it) should be this:
Code:
#!/usr/bin/env bash

That line will cause the process that opens your script for execution to execute it using bash. It is a way of ensuring that bash is used, even if the shell you use for your comand line isn't bash.

I hope that makes some sense.

The logic of the code I posted is this:

1) Read all records from the p1 file into an array named p1v (p1 values). If there are 4 records in the file there will be 4 values in p1v (p1v[0], piv[1]... p1v[4]).

2) Do the same for p2 -- read all values into p2v.

3) After reading values into p2v, the variable i will contain the number of records read.

4) finally, for each name/number pair entered, loop from 0 to i and print the desired lines using the corresponding p1v and p2v values.

Hope this helps
This User Gave Thanks to agama For This Post:
# 5  
Old 08-14-2011
Please help me out with this script

Hi,

I have tried to write one more code here for the same output by taking all the inputs in one file.

Please check


My input file

Code:
bash-3.00# cat /tmp/t
1133    000E
113B    000F
1143    0010
114B    0011

Code is
Code:
#echo -n "enter the number of ports"
#read N
#for (( i=1;i<N;i++ ))
#do
echo -n "enter the director name :"
read NAME;
echo -n "enter the port num :"
read NUM;
cat  /tmp/t | awk '{print "map dev " $1 " to dir " '"$NAME"'":"'"$NUM"' " target=0, lun=" $2";"}'
#done

My output

Code:
bash-3.00# sh /tmp/new
-n enter the director name :
10c
-n enter the port num :
1
map dev 1133 to dir 10:1 target=0, lun=000E;
map dev 113B to dir 10:1 target=0, lun=000F;
map dev 1143 to dir 10:1 target=0, lun=0010;
map dev 114B to dir 10:1 target=0, lun=0011;

The only problem is this when i am entering the input "10c" the ourput should be 10c but it comes only 10 so where is the "c" i am not able to find it.

and my for loop is also not working same error repeating that syntax is not right.

thanks in advance.

Regards
Nirjhar

Last edited by zxmaus; 08-15-2011 at 12:00 AM.. Reason: added code tags
# 6  
Old 08-14-2011
Quote:
Originally Posted by nirjhar17
Hi,

I have tried to write one more code here for the same output by taking all the inputs in one file.
This seems much better to me.

Quote:
Code:
Code is
#echo -n "enter the number of ports"
#read N
#for (( i=1;i<N;i++ ))
#do
echo -n "enter the director name :"
read NAME;
echo -n "enter the port num :"
read NUM;
cat  /tmp/t | awk '{print "map dev " $1 " to dir " '"$NAME"'":"'"$NUM"' " target=0, lun=" $2";"}'
#done

The looping issue might be that you either need to initialise i to 0, or change the conditional to i <= N.

You are seeing the 'c' dropped because of the way you are quoting your awk. I personally believe that it's never good form to open and close quotes to include shell variables in your awk. This is the way I would write it:

Code:
awk -v num=$NUM -v name=$NAME '
    {
        printf( "map dev %s to dir %s:%s target=0, lun=%s;\n", $1, name, num, $2 );
    }
' </tmp/t

Notice that there is no need for using cat as awk is capable of reading directly from the file. Also, not having the single quotes broken, you don't run into any of the issues that you were seeing before.

When you ran your script the print statement ended up looking like this after expansion of the shell variables:
Code:
+ awk '{print "map dev " $1 " to dir " 10c":"1 " target=0, lun=" $2";"}'

You should notice that the 10c is unquoted, so awk will interpret it as a number (awk silently drops the trailing letters) and that is why it was printing 10 instead.
This User Gave Thanks to agama For This Post:
# 7  
Old 08-14-2011
Please help me out with this script

Hi,

Thanks for the help and support.


Regards
Nirjhar
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question