Command works at the command-line, but won't work in a shell script

I am creating a script and when I try to capture a command return, I have an error of command not found, if I use this command on the terminal:

gcloud -q compute snapshots list --format='csv(NAME)' 

It works fine.

The script is:

#!/bin/sh CSV_SNAPSHOTS= $(gcloud -q compute snapshots list --format='csv(NAME)') IFS=$'\n'  for i in $CSV_SNAPSHOTS do     echo "$i" done 

Replay

There must not be any whitespace after = (and also before =) in variable declaration.

So this should do:

CSV_SNAPSHOTS=$(gcloud -q compute snapshots list --format='csv(NAME)')

Also note that, you should (almost always) quote variable and command substitution, although you would get away in this case as you are saving the command substitution to a variable.



Example:

$ foo="$(echo spam)"
$ echo "$foo"
spam

$ bar= "$(echo egg)"
No command 'egg' found, did you mean:

The error is the space after the =, but you could also bypass storing the output in a variable and instead read it directly into your loop:

IFS=$'\n'

gcloud -q compute snapshots list --format='csv(NAME)' |
while read -r i; do
    printf "%s\n" "$i"
done

Category: shell script Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.131 (s). 12 q(s)