Read command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read command
# 1  
Old 05-17-2010
Read command

Hi everyone,

I have problem while writing a shell script for linux (Red Hat).

First I need to create a read command. I tried to google this but so far I can't sort this out. I hope you will be able to help me.

I have to read a file like this :

GESTION_DATA_SET_variable1_variable2

Variable1 could only be : ELS or IMA or NPI or NPD

variable2 could only be : 0 or 1 or 2

so I am writing step by step a script do to different things but fisrt the script should read the file wanted.

Code:
#!/bin/bash

# INSTRUMENT

instrument_name=$4
if [ "$instrument_name" == "" ]
then
   echo "Instrument Name (usage ELS ; IMA ; NPI ; NPD) : \c"
   read instrument_name
fi

# PHASE

phase_name=$5
if [ "$phase_name" == "" ]
then
   echo "phase name is numeric value (usage 0 ; 1 ; 2) : \c"
   read phase_name
fi


#------------------------------------------------------
# fonction

jboss () {
        rdate=$(cat GESTION_DATA_SET_${4}_${5} | awk '{printf "%s\n" ,$2"_"$3}')
        echo ${rdate}

}

#------------------------------------------------------

jboss

sleep 2

exit

so far the script is unable to complete the file name correctly as the output screen shows error that No such file or directory : GESTION_DATA_SET__

I hope you will be able to help a such dummies : )
# 2  
Old 05-17-2010
Insted of $4 and $5 use "$instrument_name" and "$phase_name".

i.e :

Code:
rdate=$(cat GESTION_DATA_SET_${instrument_name}_${phase_name} | awk '{printf "%s\n" ,$2"_"$3}')


Smilie
# 3  
Old 05-17-2010
Dear Reboot,

thanks for your help. here is what I have done. So far this work.

Please note I made a mistake, the script have to be in ksh.

Code:
#!/bin/ksh


#  instruments
liste_instruments[1]="ELS"
liste_instruments[2]="IMA"
liste_instruments[3]="NPI"
liste_instruments[4]="NPD"

#  phase
liste_phase[1]="0"
liste_phase[2]="1"
liste_phase[3]="2"
liste_phase[4]="3"


# INSTRUMENT

if [ "$id_instrument" == "" ]
then
   echo "id_instrument (usage ELS ; IMA ; NPI ; NPD) : \c"
   read id_instrument
fi

# PHASE

if [ "$id_phase" == "" ]
then
   echo "phase name is numeric value (usage 0 ; 1 ; 2 ; 3) : \c"
   read id_phase
fi

############################################################################
fct_verif_inst ()
{
#
# Check Instrument
#
i=1
while [ $i -lt 5 ]
do
  if [ $id_instrument = ${liste_instruments[$i]} ]
  then
    break
  else
    let i=$i+1
  fi
done
if [ $i -eq 5 ]
  then
  echo "   instrument \""$id_instrument"\" not valid (ELS, IMA, NPI ou NPD)"
  echo " "
  fct_usage
fi
}

echo "      Instrument  : "$id_instrument

############################################################################
fct_verif_phs ()
{
#
# Phase mission
#
i=1
while [ $i -lt 5 ]
do
  if [ $id_phase = ${liste_phase[$i]} ]
  then
    break
  else
    let i=$i+1
  fi
done
if [ $i -eq 5 ]
  then
  echo "   Phase mission \""$id_phase"\" not valid (0, 1, 2)"
  echo " "
  fct_usage
fi
}

echo "      Phase de la mission  : "$id_phase




############################################################################

        rdate=$(cat GESTION_DATA_SET_${id_instrument}_${id_phase} | awk '{printf "%s\n" ,$2"_"$3}')
        echo ${rdate}


#------------------------------------------------------



sleep 2

exit

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read command

Trying to use the read command. How do you add a 2nd option? In this example I'd like to offer two options, pre and post. If you answer pre, you get one output but if you answer post, you get another output. echo Is this pre or post? read pre if then echo You have typed pre. fi (6 Replies)
Discussion started by: jimmyf
6 Replies

2. UNIX for Beginners Questions & Answers

Need help with read command

Is there a way to make the input of the read command (or some similar command that I'm unaware of) not visible, or with an astrix?? An example: #!/bin/bash # Example echo; echo "Who are you??"; read name if ; then echo "Welcome, the terminal is yours."; exit else "Stranger... (2 Replies)
Discussion started by: Huitzilopochtli
2 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

read command

Hello guys, I am trying to a script that reads from key board and use the entered value in the next step. Example: enter folder name read $folder (i will give work) cd /main/$folder/ pwd it should print /main/work ---------- Post updated at 03:31 PM ----------... (1 Reply)
Discussion started by: sharath24
1 Replies

5. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

6. Shell Programming and Scripting

read command

Is there a way to use the READ command and force the user to enter a non-zero length string? If the user enters a zero length string the user input is rejected. code: print "what is the answer: \n" read answer (2 Replies)
Discussion started by: djehresmann
2 Replies

7. Shell Programming and Scripting

read command in while

hi all iam not able use read command in the while loop in the following program while read line do echo $line echo "enter name" read name echo "your have entered $name" done < work.txt THE READ COMMAND INSIDE THE WHILE LOOP IS NOT WORKING, IS ANY OTHER WAY TO SOLVE THIS... (7 Replies)
Discussion started by: avi.skynet
7 Replies

8. Shell Programming and Scripting

Regarding read command

Hi all, What does -u option indicates in read command. while read -u var1 do . . done < file.txt (1 Reply)
Discussion started by: krishna_gnv
1 Replies

9. Shell Programming and Scripting

read command

I have file which is space filled likE below Note: here spaces are replaced by |. When I use read command to read this file all the spaces are truncated only the default space is not removed. The output is Note: here spaces are replaced by |. Can this default truncation be override? (4 Replies)
Discussion started by: COD
4 Replies

10. Shell Programming and Scripting

read command

'Morning vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}'|read var echo $var This syntax run on AIX (ksh) but not on linux (bash). I think that problem is the read command, because the following syntax is ok : vmstat 1 1|sed 1,2d|awk '{printf("%s\n",$1)}' Could someone help me! regards... (16 Replies)
Discussion started by: nymus7
16 Replies
Login or Register to Ask a Question