This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
Closed 4 years ago.
I written the following script which gets name of a file and then assemble and link the file. But it doesn't work. What is the problem with it?
EXPECTED_ARGS=2
if [ $# -ne $EXPECTED_ARGS ]
then
echo "[+] Assembling with Nasm."
nasm -f elf32 $1 -o $1.o
echo "[+] Linking ..."
ld $1.o -o $1
echo "[+] Done!"
else
printf "\nInvalid number of arguments, please check the inputs and try again\n"
fi;
When I run it without passing any args, it doesn't shows following error:
printf "\nInvalid number of arguments, please check the inputs and try again\n"
Ok, try like this
define a variable ARGC=$#
and you if statement will look like
if [ $ARGC -ne $MAX_ARGS ]; then
Legend:
-ne = not equal
-gt = greater than
-eq = equal to
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
using OR in shell script
(2 answers)
Closed 7 months ago.
here is my simple script prova.sh:
#!/bin/bash
echo "\$# = [$#]"
echo "\$1 = [$1]"
if [ $# -ge 2 || $1="-h" ]
then
echo "#########################################################################"
echo -e "usage: $0 \t launches the program only"
echo -e " $0 -s\t shows the command fired and launches the program"
echo -e " $0 -so\t shows only the command fired"
echo -e " $0 -h\t shows this guide"
echo "#########################################################################"
exit 0
fi
command="ls -la"
if test "$1"="-so" || "$1"="-s"
then
echo "Fired command:"
echo $command
fi
if test "$1"!="-so"
then
$command
fi
here is the output:
$> ./prova.sh -h
$# = [1]
$1 = [-h]
./prova.sh: row 6 : [: "]" missing
./prova.sh: row6: -h=-h: command not found
Fired command:
ls -l
totale 4
-rwxrw-r--. 1 user user 632 20 lug 16.13 prova.sh
I expected just the help
you need to close every test using single brackets, the shell way, if [ $# -ge 2 ] || [ $1 = "-h" ]; then ... this will fix it. If you still use single brackets way, is safe to enclose you variables with double quotes, so if they are empty, you are safe, like this if [ "$#" -ge 2 ] || [ "$1" = "-h" ]; then ... or you can just put them inside a double bracket, and bash will handle this for you if [[ $# -ge 2 || $1 = "-h" ]]; then ...
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
here is my script:
#!/bin/bash
read -p "para:" teatp
if [ -z $teatp ]; then
echo '-z is ture'
else
echo '-z is false'
fi
if [ -n $teatp ]; then
echo '-n is ture'
else
echo '-n is false'
fi
when I input nothing and press enter, the result is
para:
-z is ture
-n is ture
on the other hand, when I input something and press enter, the result is
para:qwer1234
-z is false
-n is ture
which confused me is the first result -n is ture.
I think the -n and -z are antonyms, but why is the result the same?
There must be something I ignore or misunderstand.
I will be appreciate if someone can point out
the #testp should be quote by double quotation marks?
like:
[ -n "$teatp" ]
This question already has answers here:
How to test if string exists in file with Bash?
(16 answers)
Closed 3 years ago.
Verify if base machine IP is present in file
Trying to resolve it using for loop with if condition
#!/bin/bash
#file with IP List /tmp/ip_list.txt
curr_ip="$(hostname -I)"
for n in `cat /tmp/ip_list.txt`;
do
if [ "$n" == "$curr_ip" ]; then
echo "success"
else
echo "fail"
fi
done
By default it is running else condition.
#!/bin/bash
curr_ip="$(hostname -I)"
output=$(grep -c /tmp/ip_list.txt)
if [ "$output" != "0" ]
then
echo "success"
else
echo "failure"
fi
The -c option in grep gives you a count. If it doesn't equal zero, it found something.
I'm developing simple terminal based application that helps to compile your C/C++ and Python source files in a one command. But when I execute the function like 'erun test.py' It's always gives the output: ERun: file unknown file extension.
For my opinion the problem at the if statement. I try to edit these statements but nothing is changed. Here is my source code:
#/bin/bash
# function ERun for C/C++ and python
# version 1.0
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function erun {
if [ -z "$1" ]; then
#display usage if no paramters given
echo "Usage: erun file.c/.cpp/.py"
echo "Run: ./file"
else
for n in "$#"
do
if [ -f "$n" ] ; then
case "$n{n%,}" in
*.py)
chmod +x "$n" ;;
*.c|*.cpp)
gcc "$n" -o "$n" ;;
*)
echo "ERun: '$n' unknown file extension"
return 1
;;
esac
else
echo "'$n' - file does not exist."
return 1
fi
done
fi
}
IFS=$SAVEIFS
My expected output is getting a executable file. I'll be happy if you can help me. By the way if you want to contribute my this tiny project here is the project link: https://github.com/lvntky/ERun/ :)
This is weird
"$n{n%,}"
For ab/program.py, it returns ab/program.py{%n,}.
You probably wanted something like
"${n,,}"
instead which turns all the uppercase letters to lowercase.
This question already has answers here:
What does the "$#" special parameter mean in Bash?
(2 answers)
Closed 8 years ago.
I have a script with this:
login {
# checking parameters -> if not ok print error and exit script
if [ $# -lt 2 ] || [ $1 == '' ] || [ $2 == '' ]; then
echo "Please check the needed options (username and password)"
echo ""
echo "For further Information see Section 13"
echo ""
echo "Press any key to exit"
read
exit
fi
} # /login
But I really dont know what the $# means on the third line.
The pound sign counts things.
If it's just $#, it's the number of positional parameters, like $1, $2, $3. (Not counting $0, mind you.)
If it's ${#var}, it's the number of characters in the expansion of the parameter. (String length)
If it's ${#var[#]}, it's the number of elements in the array. Since bash arrays are sparse, this can be different from the index of the last element plus one.
It's the number of arguments passed.
You can read it here, search for "Detecting command line arguments"