I am trying to execute the command
dirFiles=( * )
if [ ! -e $dirFiles ]; then
echo "NO FILES HERE"
fi
However, bash is throwing the error:
bash: [: too many arguments
I'm not quite sure why this error is coming about, if anyone could shed some light on this it would be appreciated.
Because you're doing it wrong in the first place.
shopt -s nullglob
if [ "${#dirFiles[#]}" -eq 0 ] ...
Related
I'm trying to script an automatic md5sum check for my embedded system running uClinux.
The script is generated on my computer as well as the tar file I want to check.
The script goes like this :
#!/bin/sh
filename='My_File'
md5='d4deeac6f655ee5d4b9ec150fc6957a5'
if test ! -e $filename.tar
then
echo Update file does not exist
exit 1
fi
if [ -z `md5sum "$filename.tar" | grep $md5` ]
then
echo 'md5sum is not correct'
exit 1
else
echo 'md5sum is correct'
fi
tar -xvf "$filename.tar"
[...]
The md5sum check run as expected, i-e the script stops when the checksum is wrong and executes to the end otherwise. But when the checksum is correct, I get this message from the console :
[: My_File.tar: unknown operand
I don't understand why I get this, and I think this is not accurate to let my script like this. Can someone explain me what's the shell is doing and how to get rid of this message ?
Thanks
Quote the output of md5sum so it's not split into multiple words. -z only expects one operand.
if [ -z "`md5sum "$filename.tar" | grep $md5`" ]
While we're here, might as well switch to the nicer $(...) syntax.
if [ -z "$(md5sum "$filename.tar" | grep $md5)" ]
So I have this in my .sh file:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
make OS=APPLE -k
if [$? -eq 0]
then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi
The problem is, bash prints this message I don't even understand:
./build-mac.sh: line 7: [0: command not found
Any help or advice is appreciated!
I'm running on a Mac with Bash.
You need to add additional spaces on the if line around the square brackets. Change your code to:
if [ $? -eq 0 ]
The reason this is required is that [ is a command itself (a synonym on *nix for test) and you need to execute the [ command not the (non-existing) [$? command.
Might as well not use $? at all:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
if make OS=APPLE -k ; then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi
I am try to create a symlink using ssh. Here the commands I am using.
cd /home
for homedir in *
do if [ -d ${homedir} ]
ln -s /etc/cpbackup-exclude.conf ${homedir}/cpbackup-exclude.conf
fi
done
After I enter fi I get this error. "-bash: syntax error near unexpected token 'fi'
I am not sure what is wrong?
Thank you for your help
The syntax of the if command is:
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
Add keyword then to your code:
if [ -d ${homedir} ]
then
ln -s /etc/cpbackup-exclude.conf ${homedir}/cpbackup-exclude.conf
fi
BTW, you can indent your code to make it easy to read.
I think you might need the keyword then after condition in the if statement.
See documentation here.
I googled for this, but I can't figure out why Bash complains with the following code to check if a directory exists:
test.mk
#!/bin/bash
MYDIR="dl"
all:
if [ ! -d $MYDIR ]; then
#if [ ! -d "${MYDIR}" ]; then
#if [ ! -d ${MYDIR} ]; then
#Here
fi
make -f test.mk
if [ ! -d YDIR ]; then
/bin/sh: Syntax error: end of file unexpected
make: *** [all] Error 2
Does someone know why it fails? And why does it call /bin/sh instead of /bin/bash? Thank you.
Edit: unlike Bash, make doesn't support multi-line block. Here's working code:
MYDIR="dl"
all:
if [ ! -d ${MYDIR} ]; then\
echo "Here";\
else\
echo "There";\
fi
The #!/bin/bash shebang that you inserted at top is useless, and it is treated by make as a comment.
make sends by default commands to /bin/sh. To specify a different shell, use the macro SHELL = /bin/bash.
Moreover, you need to escape your variable:
if [ ! -d ${MYDIR} ]
I'm not sure if make can handle multi-line statements, so try to put all the if block in a line.
if [ ! -d ${MYDIR} ]; then DO_SOMETHING; DO_SOMETHING_ELSE; fi
You're feeding test.mk to make, not to bash. Then make sends individual lines to the shell, not whole blocks.
make uses its SHELL macro to determine which shell to use. You can override it to make it use bash.
The reason why you're getting YDIR is that make has silly rules about variable interpolation. Write $(MYDIR), not $MYDIR.
try bracing your variable:
${MYDIR}
Well, I am currently a novice shell scripter. I have a basic knowledge of other programming languages such as Javascript for HTML, JScript, VB.NET, and C++. (C++ Not so much, just dipped my feet in the water for that). Most recently, my recent project has been attempting to learn as much as I can about Bash shell scripting. The current issue is understanding status error codes, as well as checking if certain parameters contain directories or if it exists at all. I am working from a book, with tutorials and review questions. Unfortunately there is no answer key, or even hints for that matter.
Display an error message and exit with status 1 if no parameters are given
Display an error message and exit with status 2 if source is not a directory
Display an error message and exit with status 3 if destination does not exist or is not a directory
The above is what I must do, so far the first one I believe I have done correctly, if not please, correct me, or guide me in the proper direction. As I learn best when samples are given, I thought I would ask for assistance.
if [ $? ]; then
echo "You must supply at least one parameter"
exit 1
fi
#The above is the part I am pretty sure is correct.
if [ $? -d $directory "$1" ]; then
echo "$directory is not a directory"
exit 2
fi
#The above was self written. I am almost positive it is wrong.
if [ $? -lt 2 ]; then
set "$1" .pwd
fi
#the above was given to me from the book as a reference point to start (tutorial)
$? is the return code of a command you execute. Maybe you are thinking it's a return code of the current script. In any case it's not doing what you think it's doing.
All of my examples assume your command is run like so: script [source] [destination]
Error message if no parameters are given:
if [ ! "$#" ]; then
echo "please supply a parameter"
exit 1
fi
Display error if source is not a directory
if [ ! -d "$1" ]; then
echo "$1 is not a directory"
exit 2
fi
Display error if destination doesn't exist or isn't a directory
if [ ! -d "$2" ]; then
echo "$2 doesn't exist or isn't a directory"
exit 3
fi