How to display argument in shell script - linux

I have a shell script called displayArg.sh This is how I intend to run it-
./displayArg hello
and the output is entered arg is hello
The following is the script-
if [ $1 == "" ]; then
default="Default"
echo "no value is given. Output is $default"
else
value=$?
echo "entered arg is $value" #I know I am wrong in these 2 lines, but not sure how to fix it
fi
Kindly bear with me. I'm new to Shell scripting

You want:
value="$1"
($? is the status of the last command, which is 1 because the test command is what was executed last.)
Or you can simplify to:
if [ "$1" == "" ]
then
echo "no value is given. Output is Default"
else
echo "entered arg is $1"
fi
Note the quotes around "$1" in the test. If the string is empty, you get a syntax error. Your alternative with bash is to use a [[ $1 == "" ]] test.

Related

Linux Help keep getting new line error for line 6 dont know what the problem is

#!/bin/sh
if ($#argv == 0)
echo There are no arguments
if ($#argv != 0)
echo There are $#argv arguments
I'm not surprised you're getting errors; you have several syntax errors and incorrect constructs that would prevent your code from running on the Bourne shell (the interpreter indicated by your shebang line). I've fixed your code below so sh will actually run it:
#!/bin/sh
if [ "$#" == "0" ]; then
echo "There are no arguments"
fi
if [ "$#" != "0" ]; then
echo "There are $# arguments"
fi
That being said, you're testing for the same condition twice ("$#" == "0" / "$#" != "0"); I would refactor this to just a simple if/else instead of the two separate conditionals you have currently.

How to get the output of if condition on terminal

I have this example and i want to get on screen both condition and good
Code:
if [ `echo condition` ]; then echo good; fi
The output that i want to get:
condition
good
The output that i got:
good
As the command echo condition will be replaced with another command. The if statement must check the return code of the condition command.
Simply store it in a variable:
cond=$(echo condition)
if [ "$cond" ] ; then
echo "$cond"
echo good
fi
Do not use backticks anymore nowadays.
Store the output of your sub shell in a variable:
if condition=$(echo condition); then
echo "$condition"
echo good
fi
If you want to return the exit value of the sub shell, you have to write a function:
get-condition-result()
{
local condition
local result
if condition=$(echo condition); then
result=$?
echo "$condition"
echo good
else
result=$?
fi
return "$result"
}
Solution found here: https://stackoverflow.com/a/13343457/12953642
You can run your command without any additional syntax. For example, the following checks the exit code of grep to determine whether the regular expression matches or not:
if echo condition
then
echo "good"
fi
if echo "condition" && curl
then
echo "good"
else
echo "error in command"
fi
Output:
condition
good
condition
curl: try 'curl --help' or 'curl --manual' for more information
error in command

Why doesn't string comparison with wildcards work properly?

I wrote this shell code, but it doesn't get the good output.
Even though the $csoport gets the "...: No such user" output, id doesn't echoes the following line I wrote there.
read felhasznalo
while [ "$felhasznalo" != "exit" ]
do
csoport=`groups $felhasznalo`
echo "$csoport"
if [[ "$csoport" == *": No such user"* ]] ; then
echo -n "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
echo -n "Felhasznalo: "
read felhasznalo
done
You shouldn't try to match the error messsage since you only care if groups fails. You ought to do:
if ! csoport=$(groups "$felhasznalo"); then
printf "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi

Run Linux shell script with arguments

Can somone help me with this: so i have this script
#!/bin/bash
echo -n "Enter a value for X:(999 to exit): "
read x
until [[ $x == 999 ]]
do
echo -n "Enter a value for Y: "
read y
echo "X="$x
echo "Y="$y
((a=y+x))
echo "X+Y="$a
((s=y-x))
echo "X-Y="$s
((m=y*x))
echo "X*Y="$m
((d=y/x))
echo "X/Y="$d
((m=y%x))
echo "X%Y="$m
echo -n "Enter a value for X:(999 to exit): "
read x
if [[ $x == 999 ]];
then
exit 0
fi
done
exit 0
but i didnt know how to write the rest of it, the missing thing is:
Use the two command line arguments when the script starts if the user supplied them, and then prompt for more numbers to continue in the loop.
Am guessing the arguments you are looking from the user are x and y values. The easiest way to check if user provided arguments is to use $# which gets you the number of arguments given by the user.
So use it like this:
if [ "$#" -eq 2 ]; #2 arguments provided by user
then
x=$1
...
fi

How to catch missing argument in function mistake?

I have just made the following mistake, where I am passing an argument to a function which is empty.
var1="ok"
var2=$notDefined
func $var1 $var2
func() {
var1=$1
var2=$2
echo $var1
echo $var2
}
For each argument in the function I could do
if [ -z $1 ]; then echo "Empty argument"; fi
But is there a more generic method to do this, so it is easy reusable, and would perhaps even tell the variable name that is empty?
You can stop whole script by set -u. It will fail if you try to use unset variable. It is very general approach.
Bash will output following localized message to standard error:
bash: x: unbound variable
You want to use the ? bash variable substitution operator:
var1=${1:?"undefined!"}
If $1 exists and isn't null, var1 is set to its value, otherwise bash prints 1 followed by "undefined!" and aborts the current command or script. This syntax can used for any bash variable.
In your case the empty variables are created, because there are too few arguments to the function.
You can get the number of passed arguments via $#. All variables that use $n with a higher number n must then be empty. You could check for a sufficiently high number of arguments at the beginning of your function.
#!/bin/bash
var1="ok"
var2=$notDefined
func() {
if [[ $# -ge 2 ]]; then
var1=$1
var2=$2
echo $var1
echo $var2
else
echo "Missing values"
fi
}
func $var1 $var2
Here it is running
./test.sh
Missing values
Here it is with two values:
#!/bin/bash
var1="ok"
var2="dokie"
func() {
if [[ $# -ge 2 ]]; then
var1=$1
var2=$2
echo $var1
echo $var2
else
echo "Missing values"
fi
}
func $var1 $var2
Results with:
./test.sh
ok
dokie

Resources