The following piece of code is not running because it is fails to go inside the if condition. i want to create a directory if there is no directory in the input path. i am using Linux, by CENT. Please help.
echo " Enter the path where you u want to extract the tar"
read EXTRACT_PATH
ls -ld $EXTRACT_PATH 2>>/dev/null | if [ $? -ne 0 ]
then
mkdir $EXTRACT_PATH
echo "new directory creadted......"
fi
echo " Enter the path where you u want to extract the tar"
read EXTRACT_PATH
if [ ! -d $EXTRACT_PATH ]
then
mkdir $EXTRACT_PATH
echo "new directory creadted......"
fi
You are doing an ls -ld directory_name, and then checking the exit status. The ls -ld command will always (I think anyway - test for yourself) return a 0 (zero) for the error condition. The ls can run, and you are sending errors elsewhere anyway.
Perhaps a better way would be something like
if [ -d $EXTRACT_PATH ]
then
echo "yep, a directory"
else
echo "no, it is not"
fi
Piping into the if is also a problem; by definition, the exit status of a pipeline is the exit status of the last command of the pipeline, so false; false | if [ $? == 1 ] is not well-defined (and at least in my Bash, doesn't do anything; the error code is always zero at that point).
Thank you guys, all of you have given very relevent and precise answers to my query.
although i am a J2EE app developer, and some how i am into UNIX now and that was first shell script, so i had lot of doubts, and u guys cleared it now.
joeyg,
reason being i was doing ls -ld directory_name is, if there is a directory then it gives exit staus 0 and if not it gives 1, based on this value i was creating a directory.
but
era, is write pipeline is treated as command by shell and if it is feeded to "if" statement it always gives exit 0.
thanks era.
Franklin52
i am making a Debian package to install and configure sun directory, opensso, openssl, so there i have to check at some places whather directory exists or not if not then i have to make it for the installation purposes.
thanks guys, i will look forward to your valuable help again.
cheers bye.
I think Franklin's point was that you don't have to make it a conditional at all; mkdir -p /tmp will run fine even if /tmp already exists, so basically that's the whole script for you.
is the following way is the right to check a file in a folder and if file not in the folder copy from the repositry.
cd /var/lib
ls -l libstdc++.so.5 >>/dev/null
if [ $? -ne 0 ]
then
cp repository path
echo "new file copied......"
fi
Not really, no. Using ls and redirecting to /dev/null is not particularly elegant (and using the -l option to then just throw away the results is rather wasteful).
test -e /var/lib/libstdc++.so.5 || cp -v repository path
The || is a shorthand for "or else". The following is closer to what you have:
if ! [ -e /var/lib/libstdc++.so.5 ]
then
cp repository path
echo "new file copied" >&2
fi
Note that test and [ are really basically the same command.
if i run a command, suppose # ./createreg -new /opt/server1
now if i have to test in script wather it executed sucsessfully, Like
./createreg -new /opt/server1
if true
do next step
else
exit the script
fi
now in my script i am doing like this
./createreg -new /opt/server1
if [ $? != 0 ] ; then echo "Instalation Error ........... Exit 127 " ; exit 1
else
proceed
fi