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.

6
  • this is similar to: stackoverflow.com/questions/1289026/…, but they don't take a command line argument, which seems to be causing my problem Commented Jan 31, 2015 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. Commented Jan 31, 2015 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. Commented Jan 31, 2015 at 19:58
  • your function is perfectly fine. pls post the entire script. Commented Jan 31, 2015 at 20:17
  • Try shellcheck.net with your script. Commented Jan 31, 2015 at 20:45

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.