Hello,
It has been a while since I started writing shell/bash scripts and it is painfully obvious I have lost my touch. Anyways, I'd appreciate it if someone can help me fix the errors in my script.
First I will post the script itself and below that the errors my terminal is giving me.
Code:
#!/bin/bash
printf "Are you in the correct directory where you want to inventory files?\n"
read -p "Y/n --> " ans
if [ $ans == y ] ;then
printf "Your Inventory will be created in a moment. Please wait...\n\n"
ls -Rl | sort -dbm > ~/Documents/Inventory.txt
elif [ $ans = Y ] ; then
printf "Your Inventory will be created in a moment. Please wait...\n\n"
ls -Rl | sort -dbm > ~/Documents/Inventory.txt
elif [ $ans = yes ] ; then
printf "Your Inventory will be created in a moment. Please wait...\n\n"
ls -Rl | sort -dbm > ~/Documents/Inventory.txt
elif [ $ans = YES ] ; then
printf "Your Inventory will be created in a moment. Please wait...\n\n"
ls -Rl | sort -dbm > ~/Documents/Inventory.txt
else
currentdir=`pwd`
printf "Please type the path to the location you would like to inventory.\nIf this path contains spaces place a back-slash in
front of EVERY space as in the example\n"
read -p "/this/is/how\ to\ make\ spaces/in/a/path/to\ a\ directory " location
cp $currentdir/inventory2.sh $location
fi
AND NOW IN MY TERMINAL...
Code:
USER@COMPUTER:/mnt/DATA/01-SCRIPTS$ sh inventory2.sh
Are you in the correct directory where you want to inventory files?
Y/n --> y
inventory2.sh: 4: [: y: unexpected operator
Please type the path to the location you would like to inventory.
If this path contains spaces place a back-slash in
front of EVERY space as in the example
/this/is/how\ to\ make\ spaces/in/a/path/to\ a\ directory /home/USER/Documents
USER@COMPUTER:/mnt/DATA/01-SCRIPTS$
According to my script
What should have happened is that after using y/Y/yes/YES the script should have created a recursive inventory of ALL my files in that directory. My issue... I used an if/then statement the way it should work yet my terminal is telling me... What exactly???
If I could get past this obstacle, after the script is copied to the correct location it will execute the proper command (which is in my script) to create a sorted inventory of files in the directory where the script has been copied...
It's been about 2 years since I did ANY shell/bash coding and I see that I am in need of serious help here...
Any pointers?
Bookmarks