ive implemented getopt for the command line but have this problem,
Code:
#!/bin/sh
text=""
set -- getopt "t" etc .... #sets arguments
while :
do
case "$1" in #gets arguments
-t: shift; text="$1" ;;
shift
done
shift
if [ $text = "" ]
then
echo "no text"
else
echo "text"
fi
when i run the program ./script -t <text>
it prints "text"
but when i just run ./script
it returns a "arguments expected" message, (shouldnt it return "no text" ?)
is there a way to fix this, i want to be able to run ./script without it complaining as well as ./script -t <text>
Since you are using case statements, you should close the case statements with esac
Your script would now look like this:
Code:
do
case "$1" in #gets arguments
-t: shift; text="$1" ;;
esac
done
Regarding your use of shift indicates that the script requires you to input arguments. From the man pages we have the following:
Code:
shift [n]
The positional parameters from n+1 ... are renamed to $1 ....
Parameters represented by the numbers $# down to $#-n+1 are
unset. n must be a non-negative number less than or equal to
$#. If n is 0, no parameters are changed. If n is not given,
it is assumed to be 1. If n is greater than $#, the positional
parameters are not changed. The return status is greater than
zero if n is greater than $# or less than zero; otherwise 0.
Also your sciprt has a $1 which actually in scipt-terminology refers to the first argument.
No arguments + your script = "arguments expected"
It is always a good idea to check whether the user has provided any arguments at all.
$# gives you the count of the number of arguments provided at command line.
You script should be tweaked like this:
Code:
#! /bin/sh
if [ $# -eq 0 ] ; then
echo "No text"
else
while :
do
case "$1" in #gets arguments
-t: shift; text="$1" ;;
shift
done
shift
echo "Text"
fi
thanks for that, that was quite informative.
just wandering how would that script be modified if say there
were multiple arguments?
e.g
arguments "t:w"
if theres no -t
do something
else
do something else
if theres no -w
do something
else
do something else
etc ...
Hi All,
I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination.
# file_copy.sh
source_dir = ${source_dir}
target = ${target_dir}
cp source_dir target
and my parameter file is like below
#parameter_file.txt
source_dir =... (4 Replies)
Hi All,
I have written a shell script which works all right on bash shell, but when it comes to execute it using ksh on AIX it gives the following error::(
bash$ /bin/ksh getShortInfo.sh
getShortInfo.sh: syntax error at line 26 : `(' unexpected
Could you please indicate what is... (4 Replies)
#include<stdio.h>
int main{
char *fl;
fl=(char*)malloc(150);
strcat(fl,"/tmp/OV/");
printf("\nInside fl--->%s\n",fl);
return 0;
}
I wrote a simple program as above. I got the error
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
Please help me out ! I am... (4 Replies)
Hello all,
I am trying to figure out why i am getting an error while executing the script...altought it seems like its work...but still get the test arguement error...any help would be appericiate...this script basically connects to any oracle db ( just have to pass db name to it)... (4 Replies)
CT=0
while read LINE
do
# Check to see if the LINE is non-empty, and has a <td> tag in it.
if
then
# Increase the TD counter by 1
CT=`echo "$CT+1"`
fi
done <test.htmthrows this error: ksh: test: argument expected
test.htm
<tr>
<td>text</td... (4 Replies)
I'm trying this script and I keep getting a 'fi' not expected error:
#!/bin/sh
#TD=0
CT=0
cat P7748 |while read LINE
do
# Check to see if the LINE is non-empty, and has a <td> tag in it.
if
# Increase the TD counter by 1
CT=`echo "$CT+1" |bc`
... (2 Replies)
Hi All
I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error.
#!/bin/ksh
#-------------------------------------------------------------------------
# File: ang_stdnld.ksh
#
# Desc: UNIX shell script to extract Store information.... (3 Replies)
Hello. Please help with this code. Returns an error message "syntax error near unexpected token =~. Conditional binary operator expected.
if " ]] || " ]]
then
echo "Enter only valid numbers"
fi (2 Replies)
Help please! :confused:
I have the following error with the following file and the emails are not arriving to the email, any idea please?
ERROR: ./launch_full_backup.sh: Syntax error at line 28 : `else' is not expected.
FECHA=`date +%d%m%y%H%M`... (2 Replies)