Help on UNIX

dear all,

Could please help me for the below request. how i can write a shell script for this. I have a 10 database which start with

 B149K, B456K, R4595, B569O, R56I9, B569I, R678K. 

The letters start with B are present in

 /database/banking 

directory and the letter start with R are present in the

/database/retailer

directory.

if i pass the input name as $1. it should it assign to the speicfic directory as mentioned. could you please help me out.

if B149K is passed , DIR=/database/banking
if R678K is passed , DIR=/database/retailer

Following code snippet would help.

DB=$(expr substr $1 1 1)
case $DB in
'B') DIR=/database/banking
     ;;
'R') DIR=/database/retailer 
     ;;
*) echo error
   ;;

Explanation :
I have a list of datbases with start with B and R.

user will giving input as B149K or R678K. if R678K is passed as input and i would write a script if the input file name start with R i should assign cdir=/database/retailer. if the input file name start with R it should assing cdir=/database/manufcatuer.

input=`echo $1 | cut -c1`
echo $cdir
if [[ $input = 'M']
then
cdir='/database/manufacturer'
else
cdir='/database/retailer'
fi

---------- Post updated at 04:25 AM ---------- Previous update was at 04:08 AM ----------

hi krishna,
thanks for your help. when i tried to print $DIR its not get printed can you help on this.

DB=$(expr substr $1 1 1)
case $DB in
'B') DIR=/database/banking
     ;;
'R') DIR=/database/retailer 
     ;;
*) echo error
   ;;

echo $DIR

---------- Post updated at 04:31 AM ---------- Previous update was at 04:25 AM ----------

Please find the error message.


+ + expr substr M84934 1 1
DB=M
tj.sh[2]: syntax error at line 11 : `$DIR' unexpected

---------- Post updated at 04:32 AM ---------- Previous update was at 04:31 AM ----------

cat tj.sh
#!/bin/sh
DB=$(expr substr $1 1 1)
case $DB in
'B') DIR=/database/manufactuer
 ;;
 'R') DIR=/database/retailer
  ;;
   *) echo error
    ;;
echo $DIR

include esac

DB=$(expr substr $1 1 1)
case $DB in
'B') DIR=/database/manufactuer
 ;;
'R') DIR=/database/retailer
 ;;
*) echo error
 ;;
esac
 echo $DIR

Hello arun888,

Could you please try following and let me know if this helps you.

cat dire.ksh
VAR=$1;
cdir=`awk -vvar=$VAR 'BEGIN{if(var ~ /^R/){print "/database/retailer"};if(var ~ /^B/){print "/database/manufcatuer"}}'`
echo $cdir

While running it following will be the output.

./dire.ksh R678K
/database/retailer

Thanks,
R. Singh

With any POSIX conforming shell, there is no need for awk , cut , expr , or any other substring operation:

#!/bin/ksh
IAm=${0##*/}
case "$1" in
(B*)	DIR=/database/banking;;
(M*)	DIR=/database/manufacturer;;
(R*)	DIR=/database/retailer;;
(*)	printf '%s: database should start with "B", "M", or "R"\n' "$IAm" >&2
	printf 'Usage: %s database\n' "$IAm" >&2
	exit 1;;
esac
printf 'Using DIR="%s" for database="%s"\n' "$DIR" "$1"
1 Like