2
function nvis()
{
    while true; do
    nvidia-smi
    sleep $1 
    done
}

I'm trying to use it like:

nvis 2

and I get an error like:

bash: syntax error near unexpected token `2'

Sorry if this is obvious; I am kind of stumped.

capybaralet
  • 1,757
  • 3
  • 21
  • 31
  • this is similar to: http://stackoverflow.com/questions/1289026/syntax-for-a-single-line-bash-infinite-while-loop, but they don't take a command line argument, which seems to be causing my problem – capybaralet Jan 31 '15 at 19:20
  • 1
    Please write a title that summarizes the specific problem. For example: Why is that bash function doesn't expect parameters ? Or something like that. – Laurent Jalbert Simard Jan 31 '15 at 19:24
  • Is that what is going on? It doesn't expect parameters for some reason? I've used similar syntax in other bash functions with no problem.... $1 is the first command line argument, no? I changed the title, but I'm not sure what the most descriptive thing would be. – capybaralet Jan 31 '15 at 19:58
  • your function is perfectly fine. pls post the entire script. – shiv garg Jan 31 '15 at 20:17
  • Try http://www.shellcheck.net/ with your script. – RedX Jan 31 '15 at 20:45
  • Thanks guys! Changing the function name fixed it. The whole script is... my .bashrc file, so I won't put it here. It could also have something to do with my lab's configuration which I also load. – capybaralet Feb 01 '15 at 23:45

2 Answers2

3

With your information, the error is not reproducible. This works fine:

#!/bin/bash

function nvis()
{
  while true; do
    echo nvidia-smi
    sleep $1 
  done
}

nvis 2
Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

I also tried your code - works fine. I will guess that your file contains 'hidden' control codes. Try:

cat -v yourfile  # OR
cat -vE yourfile

See any special codes? - remove them

Also, try:

bash -nv yourfile

:)

Dale_Reagan
  • 1,953
  • 14
  • 11