How to count and check passed arguments? - ruby

How can I translate the following Ruby code to Bash?
if ARGV.length == 0
abort "\nError: The project name is required. Aborting...\n\n"
elsif ARGV.length > 2
abort "\nError: The program takes two arguments maximum. Aborting...\n\n"
end

#!/bin/bash
USAGE="$0: <project name> [subproject attribute]"
if [ $# -lt 1 ]; then echo -e "Error: The project name is required.\n$USAGE" >&2; exit 1; fi
if [ $# -gt 2 ]; then echo -e "Error: Two arguments maximum.\n$USAGE" >&2; exit 1; fi

The following should be what you need:
#!/bin/bash
if [ $# -eq 0 ]; then
echo -e "\nError: The project name is required. Aborting...\n\n"
exit 1
elif [ $# -gt 2 ]; then
echo -e "\nError: The program takes two arguments maximum. Aborting...\n\n"
exit 1
fi
The TLDP bash guide is very good if you are looking to learn bash, see TDLP Bash guide.

Maybe:
#!/bin/bash
function functionName {
if [ $# = 0 ]
then echo "\nError: The project name is required. Aborting...\n\n"; exit 1
fi
if [ $# \> 2 ]
then echo "\nError: The program takes two arguments maximum. Aborting...\n\n"; exit 1
fi
}
functionName a

Related

Bash function always ignores the else block

When I run the below code with exact two arguments, the else block doesn't get executed.
If I take out the if else block out of the function, everything works fine.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage
In this situation, the function usage is receiving 0 arguments from the call.
Change the call to usage $#, which will pass the command line arguments to the usage function.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage "$#"

need help in bash script for if and greater than [duplicate]

This question already has an answer here:
Why do some people put a semicolon after an if condition in shell scripts? [duplicate]
(1 answer)
Closed 3 years ago.
i have
syntax error near unexpected token `elif'
`elif [[$# -gt 31]]
when i execute following script (its part from script by "Fred Weinhaus" for text-cleaner) by python 3
if [ $# -eq 0 ] then # help information
echo ""
usage2
exit 0 elif [ $# -gt 31 ] then errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---" else while [ $# -gt 0 ] do # get parameter values case "$1" in
-h|-help) # help information
echo ""
usage2
exit 0
;;
how i solve this problem?
Your script has many mistakes. My fix may not be correct.
Define "usage2".
#!/bin/bash
if [ $# -eq 0 ]; then # help information
echo ""
#usage2
exit 0
elif [ $# -gt 31 ]; then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else while [ $# -gt 0 ]; do # get parameter values
case "$1" in
-h|-help) # help information
echo ""
#usage2
exit 0
;;
esac
done
fi
There are no errors in lines no 201 and 206.
Try this code.
#!/bin/bash
usage2(){
# dummy
:
}
if [[ $# -eq 0 ]]
then
echo ""
usage2
exit 0
elif [[ $# -gt 31 ]]
then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
while [[ $# -gt 0 ]]
do
:
done
fi

How to redirect output to STDERR?

Suppose that I passed in 2 parameters to my function. How can I redirect a message to stderr?
#!/bin/bash
if [ $# = 0 ]; then
dir="."
elif [ $# = 1 ]; then
dir=$1
elif [ $# -ge 2 ]; then
echo "Too many operands." 2>> err.txt //???
exit 1
fi
Just add >&2 to the statement producing the output:
echo "Too many operands." >&2
and if you want it to be appended to a file named err.txt too:
echo "Too many operands." | tee -a 'err.txt' >&2
FWIW I'd write your code using a case statement instead of nested ifs (and tidy up a couple of things):
#!/bin/env bash
case $# in
0 ) dir='.' ;;
1 ) dir="$1" ;;
* ) printf 'Too many operands: %d\n' "$#" | tee -a 'err.txt' >&2
exit 1 ;;
esac

Semaphore thinks lock file already exists

I'm using link to create a semaphore - the idea is to lock out of writing to a db.
Here I have a script to create a table in a database:
#!/bin/bash
if [ "$#" -lt 3 ]; then
echo "Not enough parameters"
exit 1
elif [ "$#" -gt 3 ]; then
echo "Too many parameters"
exit 1
fi
if [ ! -d "$1" ]; then
echo "That database doesn't exist!"
exit 1
fi
./P.sh $1
if [ -f "$1/$2.txt" ]; then
echo "That table already exists!"
./V.sh $1
exit 1
else
touch "$1/$2.txt"
fi
./V.sh $1
echo "$3" > "$1/$2.txt"
echo "Ok, table created"
exit 0
Here's my P file:
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage $0 mutex-name"
exit 1
elif [ ! -e "$1" ]; then
echo "Target for the lock must exist"
exit 2
else
while ! ln "$1" "$1.lock"; do
sleep 1
done
exit 0
fi
and my V:
#! /bin/bash
if [ -z "$1" ]; then
echo "Usage $1 mutex-name"
exit 1
else
rm "$1.lock"
exit 0
fi
let's say I create a table by running ./create_table people footballers age,height
This should create a file footballers.lock (created by P) and then once the writing has happened the V should remove it. But for some reason the P thinks that the .lock file already exists, and it definitely doesn't.
Can anyone spot what's going wrong?
Found it - you can't use ln on directories...

Checking for the correct number of arguments

How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to make sure the command line argument actually exists and is a directory.
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.
More friendly error reporting:
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
if ! [ -e "$1" ]; then
echo "$1 not found" >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "$1 not a directory" >&2
exit 1
fi
cat script.sh
var1=$1
var2=$2
if [ "$#" -eq 2 ]
then
if [ -d $var1 ]
then
echo directory ${var1} exist
else
echo Directory ${var1} Does not exists
fi
if [ -d $var2 ]
then
echo directory ${var2} exist
else
echo Directory ${var2} Does not exists
fi
else
echo "Arguments are not equals to 2"
exit 1
fi
execute it like below -
./script.sh directory1 directory2
Output will be like -
directory1 exit
directory2 Does not exists
You can check the total number of arguments which are passed in command line with "$#"
Say for Example my shell script name is hello.sh
sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument
if [ $# -eq 1 ]
then
echo $1
else
echo "invalid argument please pass only one argument "
fi
Output will be hello-world

Resources