Read port into Bash script variable [closed] - bash

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am currently writing a bash script to automate my nodeJS deployment on my ubuntu test server. The port is listed within a the bin/www file. In the example line below the port is 3003 that I need to get in a variable of my script:
var port = normalizePort(process.env.PORT || '3003');
Do I need to parse the file with some regex in order to get the port number in a variable to work with it?

If you want to parse that 3003 out in a bash script you'll need to get the line in question, then pipe it to something that can get the value out. Something like:
grep 'normalizePort' <file_with_the_port> | grep -oE '[0-9]+'
You'll need to play around with it to get you to where it works for you.
However, as others have pointed out, that won't give you the port. That will only give you the default port. The real port is in an environment variable (that's what process.env means in this case), thus in a bash script you'd probably be able to access that PORT variable like so:
APP_PORT=${PORT:-3003}
You ought to read more on environment variables. Ideally you shouldn't be parsing ports out of files, unless those files are specifically created for storing config in a machine-friendly format like YAML or JSON.

Related

Docker compose project name hides underscore in bash but not in gitlab-CI [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
We have a docker-compose file for our infrastructure which works perfectly.
By default when I enter it in a folder /prod it will name the stack "prod", which I can see e.g. in portainer.
Now, I want the name to be with a underscore, like "infra_prod" and "infra_test".
When I do this in gitlab CI file like:
script:
- docker-compose -p "intra_test" up
It works fine.
When I log into the server by SSH with my windows powershell and enter it then in the bash:
docker-compose -p "intra_test" up
It works but the name is infratest instead of infra_test.
Same rule applies for dots.
Why?
The project name input has underscores removed by docker-compose as mentioned in this issue.
There are plans to change this behaviour as tracked by this issue.

storing remote terminal output in a variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Pseudo-terminal will not be allocated because stdin is not a terminal
I have been using similar script as of above for long. I login on remote devices, have some commands ran on them and then logout. I use telnet and ssh both for remote login. so far all the commands are being echo-ed to remote terminals and the output being saved to some log files. what I need is to be able to interpreter the output generated by the remote terminal. Consider following a command sent to remote terminal after successful login via SSH/Telnet:
echo "show system date"
after running above command, I want to store the output generated by remote machine in a variable and perform some if/else statements. So far, I didn't have any kind of success. can someone please guide me how I can save the output generated by remote terminal in a variable?
If ansible is an option you could register the output and based on it do some actions, for example:
- name: show system date
command: date
register: date
- debug: msg="{{ date.stdout }}"
You could read more about ansible and the conditionals here: http://docs.ansible.com/ansible/latest/playbooks_conditionals.html

search string and replace corresponding number in windows env [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have one file and replace string :
sample file is:
DP_SESSION_ID is a sting for values
DP_SESSION_ID is aplicat
"DP_S42SETTACC_TYPE"\1\"02"
"DP_SAP_CLIENT"\1\"460"
"DP_SAP_COMM_CONNECTION"\1\"JAVA_COMM_TOOL_ANALYZER"
"DP_SAP_CONNECTION"\1\"JAVA_TOOL_ANALYZER"
"DP_SAP_TOOLBI_CONNECTION"\1\"JAVA_TOOLBI_ANALYZER"
"DP_SESSION_ID"\1\"808"
I want search this "DP_SESSION_ID"\1\" sting and replace corresponding number like 808 in file prenatally(windows env), and i wand sing line command in windows bat command or perl command i don't want scrip or program
even i have installed cygwin tool in my server so unix also ok but single line command
server: windows 2008,cygwin x
using tool : datastage server jobs
perl -pi -e 's{" "DP_SESSION_ID"\1\"808 '"}{' "DP_SESSION_ID"\1\"900 '"'"}g' " file name
this code is not working
Please give good solution
First, you can't use single-quotes in DOS the way you can in UNIX. Second, you need to specify a backup file/extension when you use "-i" in DOS. Third, I think you skip trying to match all those complicated quotes and go with a simpler approach. It may not be the most efficient (using 2 regex) but it's very readable (to me).
This works for me:
perl -p -i.bak -e "m/\"DP_SESSION_ID\"/ && s/808/900/g" "filename"
Hope that helps!
in powershell, you can try the command as below:
get-content input.txt |foreach-object {$_ -replace '"DP_SESSION_ID"\\1\\"808"', '"DP_SESSION_ID"\1\"900"'} | set-content output.txt

Linux commands in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wont write some script in Ruby on Linux server. I need statistic from server and I'm a beginner in Ruby.
I have problem with Linux commands, because if I use exec to use Linux command, my program is fallen without error.
disks = ["sda", "sdb"]
Code:
disks.each do |disk|
puts "disk test start"
exec "smartctl -a /dev/#{disk} > /tmp/sestavy/#{disk}"
puts "disk test end"
end
Output:
[root#banan sestavy]# ruby test.rb
disk test start
[root#banan sestavy]#
Thanks
Honza
That's just what exec does: it replaces the currently running program with a new one. This is not specific to Ruby, it works the same way in the shell, in C, in pretty much any other environment.
When you use exec, it replaces the current process with what you want to execute. So it won't return to your Ruby script. See this explanation for different methods for shell execution.

Shell script with variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working a shell script to admin out email system. Essentially I get the users info and grep it to get the data I need. I've ran the below commands in terminal and they work as intended but when I use the below script I get an error "Command not found". I think its trying to run the 3rd line as a command. Anyone know what could be the problem here?
read -p "Enter email address to remove from groups: " purge_email
purge=$(python /gam/gam.py info user $purge_email)
purge_chunk=$($purge | grep -A 100 "Groups:")
echo $purge_chunk
Try:
purge_chunk=$("$purge" | grep -A 100 Groups:)
the $purge should be evaluated as formatted output the way gam kicks it out in a txt file or .csv you would use as a data source for a gam script in bash.

Resources