Ruby, Convert cron range into digit - ruby

Using Ruby
I would like that any ranges that appear in a cron to be expanded to the contained numbers in the range ex:
0,5,7,30-35 1,3-8,20 * * * /script.sh
expanded :
0,5,7,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh
Any help is appreciated

Assuming that 0,5,7,30-35 1,3-8,20 * * * /script.sh is a string, a simple gsub would work:
str = "0,5,7,30-35 1,3-8,20 * * * /script.sh"
str.gsub(/(\d+)-(\d+)/) { ($1..$2).to_a.join(',') }
#=> "0,5,7,30,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh"

Related

sed: replace part of line in text file at recognized character with entire line from another file

I have many sets of corresponding .txt files. I want to recognize a specific character ":" in the first .txt file (text1.txt) and then replace it (and the remainder of the line in text1.txt) with the corresponding line from the other .txt file (text2.txt). I would also like to add an additional character say "*" before the copied value.
text1.txt looks something like this:
*
10.04:60.429
*
*
*
12.023:60.078
*
9.033:60.045
*
9.023:60.062
*
*
and text2.txt looks something like this:
*
11
*
*
*
4
*
10
*
9
*
*
The output should look like this:
*
10.04*11
*
*
*
12.023*4
*
9.033*10
*
9.023*9
*
*
any ideas on the best way to do this with sed?
Below awk script should do
awk 'BEGIN{i=1;j=1}
NR==FNR{text1[i++]=$0;next}
/:/{gsub(/:[^:]*/,"*" text1[j],$0)}
#Note :[^:] looks for the last occurence of : in text1 strings
{j++}1' text2.txt text1.txt
Result
*
10.04*11
*
*
*
12.023*4
*
9.033*10
*
9.023*9
*
*

Create log file using crontab as different user not working [duplicate]

If I put the following in crontab -e:
* * * * * date +"%Y-%m-%d" > /home/apps/temp/env.txt
there is no env.txt created.
If I change the above line to:
* * * * * date > /home/apps/temp/env.txt
env.txt is created properly.
How can I format date in cron?
You need to escape each one of the %:
* * * * * date +"\%Y-\%m-\%d" > /home/apps/temp/env.txt
Or even better, remove the quotes and leave like this:
* * * * * date +\%Y-\%m-\%d > /home/apps/temp/env.txt

Ruby print hash in grid format

I'm working on coding a chess board. The structure of my board will be like this:
# a b c d e f g h
# 1 * * * * * * * * # <= Black pieces on top
# 2 * * * * * * * *
# 3 * * * * * * * *
# 4 * * * * * * * *
# 5 * * * * * * * *
# 6 * * * * * * * *
# 7 * * * * * * * *
# 8 * * * * * * * * # <= White pieces on bottom
I created an #board hash, which stores the value of any item in the grid (i.e. pieces or blank space).
How can I take my hash, which maps a grid location to a "*" currently (i.e. #board['a8']=> '*', etc), and output that hash in the grid-like format?
Here's my #board variable:
def drawBoard
#board = Hash.new
letter='a'
while letter <= 'h'
i=1
while i<9
#board["#{letter}#{i}"] = "*"
i+=1
end
letter=letter.next
end
#board
end
Current output is just the hash itself. i.e.,
{"a1"=>"*", "a2"=>"*", "a3"=>"*", "a4"=>"*", "a5"=>"*", "a6"=>"*", "a7"=>"*", "a8"=>"*", "b1"=>"*", "b2"=>"*", "b3"=>"*", "b4"=>"*", "b5"=>"*", "b6"=>"*", "b7"=>"*", "b8"=>"*", "c1"=>"*", "c2"=>"*", "c3"=>"*", "c4"=>"*", "c5"=>"*", "c6"=>"*", "c7"=>"*", "c8"=>"*", "d1"=>"*", "d2"=>"*", "d3"=>"*", "d4"=>"*", "d5"=>"*", "d6"=>"*", "d7"=>"*", "d8"=>"*", "e1"=>"*", "e2"=>"*", "e3"=>"*", "e4"=>"*", "e5"=>"*", "e6"=>"*", "e7"=>"*", "e8"=>"*", "f1"=>"*", "f2"=>"*", "f3"=>"*", "f4"=>"*", "f5"=>"*", "f6"=>"*", "f7"=>"*", "f8"=>"*", "g1"=>"*", "g2"=>"*", "g3"=>"*", "g4"=>"*", "g5"=>"*", "g6"=>"*", "g7"=>"*", "g8"=>"*", "h1"=>"*", "h2"=>"*", "h3"=>"*", "h4"=>"*", "h5"=>"*", "h6"=>"*", "h7"=>"*", "h8"=>"*"}
Edit:
Thanks to David's answer, he led me toward a much more compact hash generation method as well. The updated (and working) code:
def drawBoard
#board = Hash.new
('a'..'h').each do |letter|
(1..9).each do |i|
#board["#{letter}#{i}"] = "*"
print #board["#{letter}#{i}"]
end
puts
end
end
Provided that you can use symbols for the keys in #board instead of strings:
#board = {:"a1" => "*", ...}
I think the easiest would be to prepare a fixed template string, and do string format to rewrite the grids.
Template = <<_
a b c d e f g h
1 %{a1} %{b1} %{c1} %{d1} %{e1} %{f1} %{g1} %{h1}
2 %{a2} %{b2} %{c2} %{d2} %{e2} %{f2} %{g2} %{h2}
3 %{a3} %{b3} %{c3} %{d3} %{e3} %{f3} %{g3} %{h3}
4 %{a4} %{b4} %{c4} %{d4} %{e4} %{f4} %{g4} %{h4}
5 %{a5} %{b5} %{c5} %{d5} %{e5} %{f5} %{g5} %{h5}
6 %{a6} %{b6} %{c6} %{d6} %{e6} %{f6} %{g6} %{h6}
7 %{a7} %{b7} %{c7} %{d7} %{e7} %{f7} %{g7} %{h7}
8 %{a8} %{b8} %{c8} %{d8} %{e8} %{f8} %{g8} %{h8}
_
Template % #board
If you let #board be a flat array instead (which can be handled by using modulo operations), then replace the %{..} above with %s, and it will work the same way.
Here is a starting point you can use. You would need to add the row and column labels yourself, and add spaces, but this should get you going in the right direction:
('a'..'h').each do |letter|
(1..8).each do |i|
print #board["#{letter}#{i}"]
end
puts # end the line
end

rexx reading file data onto panel

can you please suggest me the manual or example codes on how i can read the content of a file to be displayed in a rexx panel.
the number of lines from the file can vary and so cannot use the static manner.
thanks,
Samuel Mathews.
In ZOS to read the file in rexx use the execio command
i.e.
"EXECIO * DISKR myindd (STEM fileContentsVar."
Reads the file into a stem variable (fileContentsVar.0 holds the number of records and
fileContentsVar.1 ... hold the actual data).
You could store the file contents in a ISPF table and display the table using the
TBDispl command
The rexx code will be roughly
address ispexec
'tbcreate myfile names(line)'
do i=1 to fileContentsVar.0
line = fileContentsVar.i
'tbadd myfile'
end
'tbtop myfile'
'tbdispl mypanel'
'tbend myfile'
For an example of a table-panel definition see http://pic.dhe.ibm.com/infocenter/zos/v1r12/index.jsp?topic=%2Fcom.ibm.zos.r12.f54dg00%2Fispzdg8040.htm
A table-panel would look like:
************************************************************
* )Attr *
* # Type(output) Intens(low) Just(asis) Caps(off) *
* )Body *
* -------------------- ????????????????? ----------------- *
* +Command ==>Cmdfld +Scroll ==>_samt+ *
* + *
* This table shows ... *
* *
* Line *
* )Model * ---- The model setion holds the
* #line + * Table display section
* *
* )Init *
* &samt=page *
* )Proc *
* )End *
************************************************************

How do I create a .p12 file in Ruby?

I've looked at the Ruby OpenSSL documentation, but I can't quite figure out the pieces I need to put together to make a .p12 file.
There's also this tutorial, but the comments belie an ambivalence about its correctness.
From ossl_pkcs12.c:
/*
* call-seq:
* PKCS12.create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]])
*
* === Parameters
* * +pass+ - string
* * +name+ - A string describing the key.
* * +key+ - Any PKey.
* * +cert+ - A X509::Certificate.
* * * The public_key portion of the certificate must contain a valid public key.
* * * The not_before and not_after fields must be filled in.
* * +ca+ - An optional array of X509::Certificate's.
* * +key_pbe+ - string
* * +cert_pbe+ - string
* * +key_iter+ - integer
* * +mac_iter+ - integer
* * +keytype+ - An integer representing an MSIE specific extension.
*
* Any optional arguments may be supplied as nil to preserve the OpenSSL defaults.
*
* See the OpenSSL documentation for PKCS12_create().
*/
So (untested and probably incorrect - I am not very familiar with Ruby):
p12 = OpenSSL::PKCS12.create("password", "key", pkey, cert)
p12_bytes = p12.to_der

Resources