In my script I am trying to error check if the first and only argument is equal to -v
, but it is an optional argument. I use an if statement, but I keep getting the unary operator expected error.
This is the code:
if [ $1 != -v ]; then
echo "usage: $0 [-v]"
exit
fi
To be more specific:
This part of the script above is checking an optional argument and then after, if the argument is not entered, it should run the rest of the program.
#!/bin/bash
if [ "$#" -gt "1" ]; then
echo "usage: $0 [-v]"
exit
fi
if [ "$1" != -v ]; then
echo "usage: $0 [-v]"
exit
fi
if [ "$1" = -v ]; then
echo "`ps -ef | grep -v '['`"
else
echo "`ps -ef | grep '[' | grep root`"
fi
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…