How to take input from the user from the command line and execute commands basedon that?

Hi,
I am using solaris 10 and bash shell.Script execution follows below.Initially it will check whether a directory exists or not if does not exist it will create it.(This I have completed)

Second step:I have four users say user1,user2,user3,user4.Script should prompt for the user id and password.then user needs to input the userid and password.That user id should lie with in the above 4 users.if it is other than the mentioned one it should say invalid user.based on the userid and password it will be executing SVN commands(I am fine with SVN commands)

only problem here is how to take the userid and password from the user in middle of script execution.based on that script should continue the execution.

I have tried this.

[echo "Please enter userid"
read userid
if [ "$userid" = "$user1" ] || [ "$userid" = "$user2" ] || [ "$userid" = "user3" ] || [ "$userid" = "user4" ]
then
cd /export/home/mvaddadi/
svn checkout  --username $userid https://scm-coconet.com/svn/repos/vomsource/ordgen/current
else
echo "invalid id"
fi]

But it is not working.I believe read is not working properly.Thanks in Advance for your inputs.

Remove the outer-most square bracket. Its not syntactically correct.

Your tests are inconsistent. You compare first $userid with variables ($user1 and $user2) then later with strings (user3 and user4).

Thanks for your input.yeah I have remove that.when i executed the script it is throwing error like this.

Please enter userid
$ mvaddadi
mvaddadi: not found
$ read userid

It is strucking up there not going ahead furteher.

---------- Post updated at 01:59 AM ---------- Previous update was at 01:55 AM ----------

May i know how can I do that?My Ultimate requirement is if the user enter input lies in between the mentioned 4 id's I need to accept it and execute some svn command.

In case if it is another userid other than the mentioned ID I should prompt sayinng invalid userid and agian prompt for Please enter correct userid.

Thanks.

echo "Please enter userid" 
read userid 
if [[ "$userid" == "$user1" ]]
then
echo "true"
else
echo "false"
fi

It is unclear what your script looks like now. Please post its current full source.

Here is the script content.

#checking for the directories and creating them accordingly
if [ -d /export/home/vomappservers/spa ]; then
 echo "directory already exist"
else 
 mkdir -p /export/home/vomappservers/spa
fi
#link to /export/home/vomappservers/spa from /spa
ln -s /export/home/vomappservers/spa spa
if [ -d /export/home/vomappservers/spa/common/5.0 ]; then
 echo "directory already exist"
else 
 mkdir -p /export/home/vomappservers/spa/common/5.0
fi
#populating the Scripts directory from SVN
cd /export/home/vomappservers/spa/common
echo "starting checkout scripts directory"
 this is the part i am concerned about.Here need to take the userinput based on the userid entered i need to execute the svn command and need to pass the user entered userid parameter to SVN command.In short with the useried enters i need to execute the svn command.It should allow only 4 users[this i can choose like user1,user2,user3,user4]
echo "Please enter your urerid  followed by [ENTER]:"
$userid=$1
svn checkout  --username $1 https://scm-coconet.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts
echo "completed https://scm-coconet.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts
 
some other commands like creating links followed

[/code]

Thanks for your response.

Please post the whole script content and only it, i.e. without broken comments or whatever.

In any case, this is bogus:

$userid=$1

Should be

userid=$1

Sorry I am giving the entire script here.It is for Build process automation.I have red color for the troubling part

#!/usr/bin/bash
 
#checking for the directories and creating them accordingly
if [ -d /export/home/vomappservers/spa ]; then
 echo "directory already exist"
else 
 mkdir -p /export/home/vomappservers/spa
fi
#link to /export/home/vomappservers/spa from /spa
ln -s /export/home/vomappservers/spa spa
if [ -d /export/home/vomappservers/spa/common/5.0 ]; then
 echo "directory already exist"
else 
 mkdir -p /export/home/vomappservers/spa/common/5.0
fi
 
if [ -d /export/home/vomappservers/spa/common/scripts ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/common/scripts
fi
if [ -d /export/home/vomappservers/spa/tools ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/tools
fi
 
if [ -d /export/home/vomappservers/spa/scm5.0/SCCS ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/scm5.0/SCCS
fi
if [ -d /export/home/vomappservers/spa/scm5.0/checkpt ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/scm5.0/checkpt
fi
echo "directories created"
 
#populating the Scripts directory from SVN
cd /export/home/vomappservers/spa/common
echo "starting checkout scripts directory"
echo "Please enter your urerid  followed by [ENTER]:"
userid=$1
read userid
if [ "$userid" = "$user1" ] || [ "$userid" = "$user2" ] || [ "$userid" = "user3" ] || [ "$userid" = "user4" ]
then
svn checkout  --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts
echo "completed https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts directory"
else
echo "invalid id please provide the valide userid"
fi
 
 
chmod 755 /export/home/vomappservers/spa/common/scripts
 
#populating the tools directory from SVN
cd /export/home/vomappservers/spa
echo"started checkout tools directory"
userid=$1
read userid
if [ "$userid" = "$user1" ] || [ "$userid" = "$user2" ] || [ "$userid" = "user3" ] || [ "$userid" = "user4" ]
then
svn checkout  --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/tools
echo "completed checkout https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/tools directory"
else
echo "invalid user id .please provide the valid id" 
 
 
chmod 755 /export/home/vomappservers/spa/tools
 
 
#exporting the.bashrc file from the SVN
cd /export/home/vomappservers/spa
echo "started exporting .bashrc file"
userid=$1
read userid
if [ "$userid" = "$user1" ] || [ "$userid" = "$user2" ] || [ "$userid" = "user3" ] || [ "$userid" = "user4" ]
then
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/.bashrc
else
echo "It is an invalid id.please enter valide userid."
 
echo "completed exporting .bashrc file"
 
 
chmod 755 /export/home/vomappservers/spa/.bashrc
#Creating the softlink to mkspadirs to mkspadirs.pl
cd /export/home/vomappservers/spa/tools
ln -s ./mkspadirs.pl mkspadirs
#creating the symolink link to /spa/common/scripts from scripts.
cd /export/home/vomappservers/spa
ln -s  /export/home/vomappservers/spa/common/scripts scripts
 
#Exporting the .SPADEF from the SVN
cd /export/home/vomappservers/spa
echo "started exporting .SPADEF file"
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/.SPADEF
echo "completed exporting .SPADEF file"
 
chmod 755 /export/home/vomappservers/spa/.SPADEF
if [ -d /export/home/vomappservers/spa/common/scripts/include ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/common/scripts/include
fi
#Exporting machdefs.pl file from the SVN
cd /export/home/vomappservers/spa/common/scripts/include
echo "started exporting machdefs.pl"
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/common/scripts/include/machdefs.pl
echo "complete exporting machdefs.pl file"
chmod 755 /export/home/vomappservers/spa/common/scripts/include/machdefs.pl
#Executing the mkspadirs script
cd /export/home/vomappservers/spa/common
./mkspadirs -d -5.0
echo $?
if [ "$?" = "0" ]
then
echo "mkspadirs executed successfully"
else
echo "There is an error in execution of mkspadirs script"
fi
#Executing the mkwk script
./mkwk 
echo $?
if [ "$?" = "0" ]
then
echo "mkmk script executed successfully"
else
echo "There is an error in execution of mkmk script"
fi
#Export of sa0_genfile,sa0_genflags,sa0_genlibs,sa0_genver_debug from SVN
cd /export/home/vomappservers/spa/5.0
echo "export of sa0_genfile started"
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/sa0_genfile
echo "export of sa0_genfile completed"
echo "export of sa0_genflags started"
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/sa0_genflags
echo "export of sa0_genflags completed"
echo "export of sa0_genlibs started"
chmod 755 /export/home/vomappservers/spa/5.0/*
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/sa0_genlibs
echo "export of sa0_genlibs completed"
echo "export of sa0_genver_debug started"
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/sa0_genver_debug
echo "export of sa0_genver_debug completed"
#creating the symbolic to sa0_genfile from genfile
cd /export/home/vomappservers/spa/common
ln -s sa0_genfile genfile
#Populating the include and scripts directory from the SVN
cd /export/home/vomappservers/spa/5.0
userid=$1
svn checkout  --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/include
svn checkout  --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/spa/5.0/scripts
chmod 755 /export/home/vomappservers/spa/5.0/*
#populating the hps.benv file from the SVN
cd /seer/nes/nete
userid=$1
svn export --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappservers/seer/nes/nete/hps.benv(can be moved to admin stuff As per the C++tem suggestion since it involves creation of directoy from root)
#Symbolic link to perl(can be done as a Admin)
ln -s /usr/bin /usr/local/bin
#Creating the Symbolic link
cd /export/home/vomappservers/spa/tools 
ln -s mkspadirs.pl mkspadirs
#Executing the spaenv script.
cd /export/home/vomappservers/spa
./spaenv
echo $?
if [ "$?" = "0" ]
then
echo "spaenv executed successfully"
else
echo "There is an error in execution of spaenv script"
fi
#Checking for the symbolic proper or not in /spa/5.0 module befor the make or build script execution.
cd /export/home/vomappservers/spa/5.0
ln -s machdefs.pl AUX_MACHINE_DEFINITION
ln -s /export/home/vomappservers/spa/common/5.0/bin bin
ln -s /export/home/vomappservers/spa/common/5.0/data data
ln -s /export/home/vomappservers/spa/common/5.0/filetran filetran
ln -s /export/home/vomappservers/spa/common/5.0/lib lib
ln -s /export/home/vomappservers/spa/common/5.0/scripts scripts
ln -s /export/home/vomappservers/spa/common/5.0/tmp tmp
ln -s sa0_genfile genfile
ln -s sa0_genflags genflags
ln -s sa0_genlibs genlibs
ln -s sa0_genver_release genver
ln -s /export/home/vomappservers/spa/tools/gen2.pl gen
ln -s /export/home/vomappservers/spa/common/5.0/obj obj
 
echo "Please enter M for make file and B for make and build file, followed by [ENTER]:"
read option_mb
if [[ "$option_mb" == "M" ]]
then
 
#command for make file
/export/home/vomappservers/spa/5.0/gen -c
else
if [[ "$option_mb" == "B" ]]
then
#command for make and build file
/export/home/vomappservers/spa/5.0/gen clean all
else
echo "Enter correct option"
fi
fi
echo "selected process is  completed"
 
#Sessmgr modification 3rd point directories creation.
if [ -d /export/home/vomappservers/sessmgr/4.0/release ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/sessmgr/4.0/release
fi
 
#Creating the symbolic link for SESSLIB 
if [ -d /export/home/vomappservers/spa/5.0/build/lib ]; then
 echo directory already exist
else 
 mkdir -p /export/home/vomappservers/spa/5.0/build/lib
fi
cd /export/home/vomappservers/spa/5.0/build/lib
ln -s /export/home/vomappservers/security/sessmgr/4.0/release SESSLIB
 
echo "Build process for SPA module is completed"
 

You didn't correct the issue I reported in post #3

Could you please let me know how can I change that to two users like you have suggested?I have made followin changes to the code to test whether at least for one userid it is allowing or not.

 #populating the Scripts directory from SVN
    41  cd /export/home/vomappservers/spa/common
    42  pwd
    43  echo "starting checkout scripts directory"
    44  echo "Please enter your urerid  followed by [ENTER]:"
    45  userid=$1
    46  read userid
    47  if [ "$userid" = "$mvaddadi" ]
    48  then
    49  svn checkout  --username $1 https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts
    50  echo "completed https://scm-coconet.capgemini.com/svn/repos/vomsource/sourcecode/vomappaservers/spa/common/scripts directory"
    51  else
    52  echo "invalid id please provide the valid userid"
    53  fi

i have just tried a dry run of the script it has thrown folloing error:

$ ./testscript.sh
directory already exist
ln: cannot create spa/spa: File exists
directory already exist
directory already exist
directory already exist
directory already exist
directory already exist
directories created
/export/home/vomappservers/spa/common
starting checkout scripts directory
Please enter your urerid  followed by [ENTER]:
mvaddadi
invalid id please provide the valide userid
./testscript.sh: line 60: echostarted checkout tools directory: command not found
if [ "$userid" = "mvaddadi" ]
1 Like