BASH throw entries in array

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1788 Views - Last Post: 23 August 2019 - 02:19 PM Rate Topic: -----

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

BASH throw entries in array

Posted 21 August 2019 - 02:45 PM

Im trying to set up an install script for nginx php, mysql, phpmyadmin and implement security features.

This is the command im running

cd /etc/php
ls

which would display something like 7.1 7.3

how would i store that into an array?
Is This A Good Question/Topic? 0
  • +

Replies To: BASH throw entries in array

#2 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 21 August 2019 - 03:21 PM

found it.

for i in $(ls); do
	echo "Value for" $i
done

Was This Post Helpful? 0
  • +
  • -

#3 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 21 August 2019 - 08:21 PM

so...i got another question...

im trying to capture the output of a simple command such as 'nginx -v'

I also need only the version number itself.

for example..

u=$(nginx -v)
var="${u:21:9}"
echo "nginx version is ${var}"



output
nginx version: nginx/1.14.2
nginx version is



if i set u to a simple string with the output of nginx -v it works.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6818
  • View blog
  • Posts: 31,416
  • Joined: 10-May 07

Re: BASH throw entries in array

Posted 22 August 2019 - 06:29 AM

I would send it to a file. Looks like for some reason -v is output to stderr & not stdout.

file=output.txt
nginx -v 2> ${file}
version=$(cat ${file})
echo ${version}


Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: BASH throw entries in array

Posted 22 August 2019 - 07:52 AM

As no2pencil points out, the problem is that the output goes to stderr. However, instead of redirecting the stderr to a file, I'd just redirect the stderr to stdout and keep capturing the output with $():

u=$(nginx -v 2>&1)


Was This Post Helpful? 2
  • +
  • -

#6 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 22 August 2019 - 07:54 AM

im still learning can you explain what i did wrong?

This post has been edited by Bobby_Bubbles: 22 August 2019 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6818
  • View blog
  • Posts: 31,416
  • Joined: 10-May 07

Re: BASH throw entries in array

Posted 22 August 2019 - 08:06 AM

the command nginx -v is sending it's output to stderr, variable=$(shell) is capturing the output from stdout, so sepp2k points out a method to redirect all output form stderr to stdout, & then it can be captured & stored into the variable.

The way that I was able to catch this was by typing on the command line nginx -v > output.txt, this sends all output (stdout) to the file. Since I still saw the version output from the command, I knew that it wasn't being printed to stdout. Thus, I ended up with an empty file, just like you get an empty variable.
Was This Post Helpful? 0
  • +
  • -

#8 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 22 August 2019 - 12:23 PM

but this part.

u=$(nginx -v 2>&1)


i dont understand the 2>&1 part.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6818
  • View blog
  • Posts: 31,416
  • Joined: 10-May 07

Re: BASH throw entries in array

Posted 22 August 2019 - 02:14 PM

It's capturing stderr (represented by the 2), & redirecting it to stdout (represented by the 1)
Was This Post Helpful? 1
  • +
  • -

#10 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 22 August 2019 - 03:29 PM

ahhhh got it.

curious question. when you punch in something like systemctl status nginx you see that green circle...how do you do that?

i was excited but i couldnt figure out how to properly insert them syntax wise. I tried putting a solid block but all it did is color the borders.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: BASH throw entries in array

Posted 22 August 2019 - 04:37 PM

To color text, you can use ANSI escapes. The escape for green as a foreground color would be \e[32m (using a syntax such as that of printf or echo -e where \e is the ESC character). To get a circle, you can just copy and paste a Unicode circle into your script. Like this one: ⬤.

So to get a green circle, this works:

echo -e '\e[32m⬤'

Was This Post Helpful? 0
  • +
  • -

#12 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 22 August 2019 - 04:42 PM

yea im not getting it read right. im on windows 7 with putty.

Is there a big solid block? and im only getting the outlines colored...i was going for something more solid.
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: BASH throw entries in array

Posted 22 August 2019 - 04:47 PM

Are you saying the circle becomes a square when you paste it into Putty? But the circle from systemctl status is displayed fine in Putty?
Was This Post Helpful? 0
  • +
  • -

#14 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 286
  • Joined: 13-March 18

Re: BASH throw entries in array

Posted 22 August 2019 - 04:54 PM

https://ibb.co/nfVn9qC
https://ibb.co/RSJj4Wf

check the difference.
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: BASH throw entries in array

Posted 22 August 2019 - 05:01 PM

I feel like you didn't specifically answer my question. What happens if you copy and paste the circle from my earlier post into Putty? Do you get the square from your first screenshot? Does the same thing happen if you copy and paste the circle from your second screenshot?

If so, maybe you're copy and pasting wrong. Note that Ctrl-V does not work in Putty. Try the middle or right mouse button.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2