problems getting arguments from console BASH

Forum rules
Before you post please read this

problems getting arguments from console BASH

Postby lmx on Sun Nov 17, 2013 5:28 pm

Hi guys...

I am making a script, but I can't have $#???its always zero :S, even when I use arguments in console...does any one know what happens ?

Is there any way to know if we have arguments others that $0 ?
god...

I am using MATE terminal...can this be a bug in Mate Terminal?

thanks in advance

regards
tux
Regards
lmx
lmx
Level 1
Level 1
 
Posts: 43
Joined: Thu Jul 14, 2011 4:11 pm

Linux Mint is funded by ads and donations.
 

Re: problems getting arguments from console BASH

Postby lmx on Wed Nov 20, 2013 1:19 pm

hi guys,

Any help on this?

Does any one have the same issue?

thanks in advance

regards
tux
Regards
lmx
lmx
Level 1
Level 1
 
Posts: 43
Joined: Thu Jul 14, 2011 4:11 pm

Re: problems getting arguments from console BASH

Postby bjornmu on Thu Nov 21, 2013 3:19 am

Your question is too vague, please explain what you are doing. Best to include a script that you have problems with.

This has nothing to do with the terminal. But if you try using $# manually on the command line, it will be 0. It's only inside a script that it gets a value.
User avatar
bjornmu
Level 3
Level 3
 
Posts: 136
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: problems getting arguments from console BASH

Postby Habitual on Thu Nov 21, 2013 10:22 am

a working example:
Code: Select all
#!/bin/bash
# Purpose:   Inserts #!/bin/bash into a file and makes it executable
# Usage:    scriptname /path/to/file.sh
# Author:    JJ/Habitual
# Date:    Tue Jul 19, 2011
# Disclaimer:   WTFPL
# Edited:       10/09/2013 10:41:52 AM EDT
MINPARAMS=1

if [ -n "$1" ]
then
echo "#!/bin/bash" > $1
echo "#################" >> $1
echo -e "# Project\t:" >> $1
echo -e "# Author\t: $(whoami| tr '[:lower:]' '[:upper:]')" >> $1
echo -e "# Date\t\t: $(date +"%a %b %d, %Y - %l:%M:%S %p %Z")" >> $1
echo -e "# License\t:" >> $1
echo -e "# Description\t:" >> $1
echo "#########i#######" >> $1
echo "" >> $1
echo "#EOF" >> $1
chmod 700 "$1"
fi

if [ $# -lt "$MINPARAMS" ]
then
  echo Usage: `basename $0` /path/to/script.sh
fi 
exit 0
My DorkBlog
Cirrhus9.com - Managed HA VDSs and Scalable grid solutions.
User avatar
Habitual
Level 7
Level 7
 
Posts: 1927
Joined: Sun Nov 21, 2010 8:31 pm
Location: Under the hood

Re: problems getting arguments from console BASH

Postby bjornmu on Thu Nov 21, 2013 1:56 pm

That if at the end works for me...

Instead if testing $#, why not just have an else part to the if [ -n "$1" ] ?
User avatar
bjornmu
Level 3
Level 3
 
Posts: 136
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: problems getting arguments from console BASH

Postby lime on Wed Dec 04, 2013 1:36 am

Dude here's just one tip

(echo "#!/bin/bash"
echo "#################"
echo -e "# Project\t:"
echo -e "# Author\t: $(whoami| tr '[:lower:]' '[:upper:]')"
echo -e "# Date\t\t: $(date +"%a %b %d, %Y - %l:%M:%S %p %Z")"
echo -e "# License\t:"
echo -e "# Description\t:"
echo "#########i#######"
echo
echo "#EOF" ) > $1

P.S. dale@cutermaster:~$ function fn { echo $#; }
dale@cutermaster:~$ fn a
1
dale@cutermaster:~$ fn a b c
3
dale@cutermaster:~$

P.P.S. dale@cutermaster:~$ bash -c 'echo $#' a b c
2
dale@cutermaster:~$
lime
Level 1
Level 1
 
Posts: 8
Joined: Sat Sep 28, 2013 6:55 am
Location: Shoreline, WA


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests