Code Monkey home page Code Monkey logo

Comments (3)

gromgit avatar gromgit commented on August 16, 2024 1

Based on your php.ini and the errors you got, I'm 100% certain that you've fallen prey to the word-splitting that happens after command substitution (see https://man7.org/linux/man-pages/man1/bash.1.html#EXPANSION for details). Remembering the order of expansions seems to be the #1 problem for most bash scripters.

I strongly suggest you use shellcheck to error-check your script. I use it religiously on all my scripts, and running it on your script surfaced almost a dozen issues, including several places where you should've double-quoted to prevent unwanted word-splitting that caused your errors. If you don't have the shellcheck binary on your system, use the online checker instead: shellcheck.net.

from jo.

gromgit avatar gromgit commented on August 16, 2024

Does anyone have an idea what I'm doing wrong or what I could do to fix it?

Not without knowing exactly how you invoked jc in your script.

from jo.

chriscroome avatar chriscroome commented on August 16, 2024

I've tried everything I can think of, for example using a pipe:

cat "/etc/php/7.4/fpm/php.ini" | jc -m --ini | jo state=present conf:=-

This works fine on it's own, you can use a minimal file for testing, for example create /tmp/php.ini

[PHP]
engine = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
[CLI Server]
cli_server.color = On

Then run:

cat "/tmp/php.ini" | jc -m --ini | jo state=present conf:=- | jq

and you get:

{
  "state": "present",
  "conf": {
    "PHP": {
      "engine": "On",
      "error_reporting": "E_ALL & ~E_DEPRECATED & ~E_STRICT"
    },
    "CLI Server": {
      "cli_server.color": "On"
    }
  }
}

But with a more complicated example:

#!/usr/bin/env bash

versions=(7.4 8.0)
sapis=(cli fpm)

jo state=present versions=$(
  for v in ${versions[@]}
  do
    echo ${v}=$(
      jo state=present $(
        for s in ${sapis[@]}
        do
          echo sapis[${s}]=$(
            cat "/tmp/php.ini" | jc -m --ini | jo state=present conf:=-
          )
        done
      )
    )
  done
)

I get:

Argument `&' is neither k=v nor k@v
Argument `~E_DEPRECATED' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_STRICT"},"CLI' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_DEPRECATED' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_STRICT"},"CLI' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_DEPRECATED' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_STRICT"},"CLI' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_DEPRECATED' is neither k=v nor k@v
Argument `&' is neither k=v nor k@v
Argument `~E_STRICT"},"CLI' is neither k=v nor k@v
{"state":"present","versions":"7.4={\"state\":\"present\",\"sapis\":{\"cli\":\"{\\\"state\\\":\\\"present\\\",\\\"conf\\\":{\\\"PHP\\\":{\\\"engine\\\":\\\"On\\\",\\\"error_reporting\\\":\\\"E_ALL\",\"fpm\":\"{\\\"state\\\":\\\"present\\\",\\\"conf\\\":{\\\"PHP\\\":{\\\"engine\\\":\\\"On\\\",\\\"error_reporting\\\":\\\"E_ALL\"}}","8.0":{"state":"present","sapis":{"cli":"{\"state\":\"present\",\"conf\":{\"PHP\":{\"engine\":\"On\",\"error_reporting\":\"E_ALL","fpm":"{\"state\":\"present\",\"conf\":{\"PHP\":{\"engine\":\"On\",\"error_reporting\":\"E_ALL"}}}

If I amend the /tmp/php.ini file so there are no spaces in sections or special characters in values (removing ~ and &), for example:

[PHP]
engine = On
[Session]
session.save_handler = files
[apc]
apc.enabled = 1

I still get:

{"state":"present","versions":"7.4={\"state\":\"present\",\"sapis\":{\"cli\":{\"state\":\"present\",\"conf\":{\"PHP\":{\"engine\":\"On\"},\"Session\":{\"session.save_handler\":\"files\"},\"apc\":{\"apc.enabled\":\"1\"}}},\"fpm\":{\"state\":\"present\",\"conf\":{\"PHP\":{\"engine\":\"On\"},\"Session\":{\"session.save_handler\":\"files\"},\"apc\":{\"apc.enabled\":\"1\"}}}}}","8.0":{"state":"present","sapis":{"cli":{"state":"present","conf":{"PHP":{"engine":"On"},"Session":{"session.save_handler":"files"},"apc":{"apc.enabled":"1"}}},"fpm":{"state":"present","conf":{"PHP":{"engine":"On"},"Session":{"session.save_handler":"files"},"apc":{"apc.enabled":"1"}}}}}}

Based on past experience I strongly suspect this is caused by an error in my poor Bash scripting...🙄

from jo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.