node.js how to put a enter - node.js

i am trying to code something in node.js but i don't know how to put a space for my log script:
Hook.info("Logger",`app: ${appopen}`)
what i want it to look like:
app: netflix
other app open: notepad
but i don't know how to put an enter so it looks like that.
so hopefully someone can help me with this!
also more information: im using https://jb3.github.io/webhook-discord/

Julien's answer should be correct. I can't yet comment but be sure that you're using backticks since those are what you need for template literals. It shouldn't be
Hook.info("Logger", "test \n ${appopen}"
but rather
Hook.info("Logger", `test \n ${appopen}`)
If you meant you wanted a carriage return at the end of the line then it should be
Hook.info("Logger", `test ${appopen}\n`)

You mean a Carriage Return, I guess. Try Hook.info("Logger", "\n")

Related

I'm not getting any output in the correct code (2nd code)?

Hi and thanks in advance for any help.
Here is the code that works with the proper output.
first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
message=(f"Hello, {full_name.title()}!")
print(message)
Here is the similar code with no output...and I can't figure out why?
first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
print(full_name)
I know it is correct coding based on the teacher's response but can't figure out why?
Thank you for any help!
You forgot to add .title() to your string formatting request.
You can do either:
# to get both capitalized add .title() twice here at "full name".
first_name="ada"
last_name="lovelace"
full_name=f"{first_name.title()} {last_name.title()}"
print(full_name)
or:
# to get both capitalized add .title() once here at "message".
first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
message=(f"{full_name.title()}")
print(message)

Getting an 'ambiguous redirect' error

I'm really new to this and I'm not understanding why it's an 'ambiguous redirect'. I have the text file "employee_data". It is specifically saying it on line 29.
Image of script
Do you actually want the literal file name employee_data?
Then write >> employee_data not >> $employee_data
If you mean something else, please clarify.

python str replace '\'

i'm new to python, i wrote script that query from db, and getting path looking like this:
...\\dev$\\AUTOMATION\\Logs\14-01-16_15-50-57_143
when i print the result i see that:
the \14
change to hexa- x0c
i want to access this link, in order to do so i need to add \ so the link will look like this:
...\\dev$\\AUTOMATION\\Logs\\14-01-16_15-50-57_143
I tried to os.path.dirname
re.find split and didnt get what i needed.
please i tried so many suggestions i saw in xda and non helped me.
Thanks,
Thanks for the response.
I know the escape , like: r'MyString', or \ when there is \
The problem is that i get the path from DB
and in the DB the path i receive is like this .. \AUTOMATION\Logs\15-01-16_15-50-57_143
and python load it immediately to x0c
I hope the problem sounds more clear..
Thnaks
When you print a string, you need to escape the backslashes with another backslash. \14 is getting read as a hex value, but \\14 will be printed correctly.

returning values in a bash function

I'm working with a growing bash script and within this script I have a number of functions. One of these functions is supposed to return a variables value, but I am running into some issues with the syntax. Below is an example of the code.
ShowTags() {
local tag=0
read tag
echo "$tag"
}
selected_tag=$(ShowTags)
echo "$selected_tag"
pulled this code from a Linux Journal article, but the problem is it doesn't seem to work, or perhaps it does and im missing something. Essentially whenever the function is called the script hangs up and does not output anything, I need to CTRL+C to drop back to CLI.
The article in question is below.
http://www.linuxjournal.com/content/return-values-bash-functions
So my question is this the proper way to return a value? Is there a better or more dependable way of doing this? And if there is please give me an example so I can figure this out without using global variables.
EDIT:
The behavior of this is really getting to me now. I am using the following script.
ShowTags() {
echo "hi"
local tag=0
read tag
echo "$tag"
}
selected_tag=$(ShowTags)
echo "$selected_tag
Basically what happens is bash will act as if the read command is taking place before the echo tag at the top of the function. As soon as I pass something to read though it will run the top echo, and complete the rest of the script. I am not sure why this is happening. This is exactly what is happening in my main script.
Change echo "hi" to echo "hi" >/dev/tty.
The reason you're not seeing it immediately is that $(ShowTags) captures all the standard output of the function, and that gets assigned to selected_tag. So you don't see any of it until you echo that variable.
By redirecting the prompt to /dev/tty, it's always displayed immediately on the terminal, not sent to the function's stdout, so it doesn't get captured by the command substitution.
You are trying to define a function with Name { ... ]. You have to use name() { ... }:
ShowTags() { # add ()
local tag=0
read tag
echo "$tag"
} # End with }
selected_tag=$(ShowTags)
echo "$selected_tag"
It now lets the user type in a string and have it written back:
$ bash myscript
hello world # <- my input
hello world # script's output
You can add a prompt with read -p "Enter tag: " tag to make it more obvious when to write your input.
As #thatotherguy pointed out, your function declaration syntax is off; but I suspect that's a transcription error, as if it was wrong in the script you'd get different problems. I think what's going on is that the read tag command in the function is trying to read a value from standard input (by default that's the terminal), and pausing until you type something in. I'm not sure what it's intended to do, but as written I'd expect it to pause indefinitely until something's typed in.
Solution: either type something in, or use something other than read. You could also add a prompt (read -p "Enter a tag: " tag) to make it more clear what's going on.
BTW, I have a couple of objections to the linux journal article you linked. These aren't relevant to your script, but things you should be aware of.
First, the function keyword is a nonstandard bashism, and I recommend against using it. myfunc() ... is sufficient to introduce a function definition.
Second, and more serious, the article recommends using eval in an unsafe way. Actually, it's really hard to use eval safely (see BashFAQ #48). You can improve it a great deal just by changing the quoting, and even more by not using eval at all:
eval $__resultvar="'$myresult'" # BAD, can evaluate parts of $myresult as executable code
eval $__resultvar='"$myresult"' # better, is only vulnerable to executing $__resultvar
declare $__resultvar="$myresult" # better still
See BashFAQ #6 for more options and discussion.

Using a Chef recipe to append multiple lines to a config file

I'm trying to create a Chef recipe to append multiple lines (20-30) to a specific config file.
I'm aware the recommended pattern is to change entire config files rather than just appending to a file, but I dislike this approach for multiple reasons.
So far the only solution I found was to use a cookbook_file and then use a bash resource to do:
cat lines_to_append >> /path/configfile
Obviously this wouldn't work properly, as it'd append the file over and over, each time you run chef-client. I'd have to create a small bash script to check for a specific string first, and, if not found, append to the file.
But this seems to defeat the purpose of using Chef. There must be a better way.
One promising solution was the line cookbook from OpsCode Community. It aimed to solve this exact problem. Unfortunately the functionality is incomplete, buggy, and the code is just a quick hack. Far from being a solid solution.
Another option I evaluated was augeas. Seems pretty powerful, but it'd add yet-another layer of abstraction to the system. Overkill, in my case.
Given that this is one of the most obvious tasks for any sysadmin, is there any easy and beautiful solution with Chef that I'm not seeing?
EDIT: here's how I'm solving it so far:
cookbook_file "/tmp/parms_to_append.conf" do
source "parms_to_append.conf"
end
bash "append_to_config" do
user "root"
code <<-EOF
cat /tmp/parms_to_append.conf >> /etc/config
rm /tmp/parms_to_append.conf
EOF
not_if "grep -q MY_IDENTIFIER /etc/config"
end
It works, but not sure this is the recommended Chef pattern.
As you said yourself, the recommended Chef pattern is to manage the whole file.
If you're using Chef 11 you could probably make use of partials for what you're trying to achieve.
There's more info here and on this example cookbook.
As long as you have access to the original config template, just append <%= render "original_config.erb" %> to the top of your parms_to_append.conf template.
As said before, using templates and partials is common way of doing this, but chef allows appending files, and even changing(editing) file lines. Appendind is performed using following functions:
insert_line_after_match(regex, newline);
insert_line_if_no_match(regex, newline)
You may find and example here on stackoverflow, and the full documentation on rubydoc.info
Please use it with caution, and only when partials and templates are not appropriate.
I did something like this:
monit_overwrites/templates/default/monitrc.erb:
#---FLOWDOCK-START
set mail-format { from: monit#ourservice.com }
#---FLOWDOCK-END
In my recipe I did this:
monit_overwrites/recipes/default.rb:
execute "Clean up monitrc from earlier runs" do
user "root"
command "sed '/#---FLOWDOCK-START/,/#---FLOWDOCK-END/d' > /etc/monitrc"
end
template "/tmp/monitrc_append.conf" do
source "monitrc_append.erb"
end
execute "Setup monit to push notifications into flowdock" do
user "root"
command "cat /tmp/monitrc_append.conf >> /etc/monitrc"
end
execute "Remove monitrc_append" do
command "rm /tmp/monitrc_append.conf"
end
The easiest way to tackle this would be to create a string and pass it to content. Of course bash blocks work... but I think file resources are elegant.
lines = ""
File.open('input file') do |f|
f.lines.each do |line|
lines = lines + line + "\n"
end
end
file "file path" do
content line
end
Here is the example ruby block for inserting 2 new lines after match:
ruby_block "insert_lines" do
block do
file = Chef::Util::FileEdit.new("/etc/nginx/nginx.conf")
file.insert_line_after_match("worker_rlimit_nofile", "load_module 1")
file.insert_line_after_match("pid", "load_module 2")
file.write_file
end
end
insert_line_after_match searches for the regex/string and it will insert the value in after the match.

Resources