How to say ${foo}123 in shell script? [closed] - bash

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 building a shell script that is checks the existence of log files in a loop. The log files I'm trying to open are named like this: access_log-%02d00-%02d59 with %02d being an hour. In perl I could just say "access_log-${hour}00-${hour}59". But how do I do that in shell script? Here's my code. It doesn't work because it thinks the var name would be $HOUR00 and $HOUR59.
for HOUR in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23; do
if [ -e tmp/access_log-$HOUR00-$HOUR59 ]; then
# do stuff here
fi
done

I figured it out myself. I works just the same as in perl. ${HOUR}00 does the trick.

Related

Get the days between two dates which are in Unix Timestamp [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
Have a file with pull-requests details and has CreatedDate (ex: 1613698170) is in Unix timestamp format.
I want to notify to stake holders, when pull-request is open more than x days.
How to get the no of days between current date and pull-request created date in bash / groovy. So that I will execute this script in jenkins and send out notifications.
The following code:
def epochMillis = 1598098239000
def now = new Date()
def then = new Date(epochMillis)
def days = now - then
println "then: $then"
println "now: $now"
println "days between now and then: ${days}"
calculates the number of days between now and epoch millis. When executed, the above prints:
─➤ groovy solution.groovy
then: Sat Aug 22 14:10:39 CEST 2020
now: Fri Feb 19 19:01:54 CET 2021
days between now and file creation: 181

Dig command repated SERVER value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I'm exploring the various options of dig command. I have disabled all output except the stats section:
$ options="+noanswer +nocmd +nocomments +stats"
$ dig example.com $options
;example.com. IN A
;; Query time: 41 msec
;; SERVER: 75.75.75.75#53(75.75.75.75)
;; WHEN: Wed Apr 01 11:59:25 MDT 2020
;; MSG SIZE rcvd: 56
$ dig stackoverflow.com $options
;stackoverflow.com. IN A
;; Query time: 49 msec
;; SERVER: 75.75.75.75#53(75.75.75.75)
;; WHEN: Wed Apr 01 11:59:43 MDT 2020
;; MSG SIZE rcvd: 110
Why these two different domains return the same SERVER? Every domain that I have tried return the same SERVER value.
;; SERVER: 75.75.75.75#53(75.75.75.75)
Is the DNS Name-server used during the dig command.
You can confirm this by checking which name-server your computer uses;
$ dig +noanswer +nocmd +nocomments +stats example.com
...
;; SERVER: 172.18.0.254#53(172.18.0.254)
...
$ cat /etc/resolv.conf | tail -n 1
nameserver 172.18.0.254
I'm using a local custom DNS server, therefore the local ip ;)

what does this command line do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I am working through this resource: https://cmdchallenge.com
On the following challenge: https://cmdchallenge.com/s/#/search_for_files_containing_string, the problem was:
Print all files in the current directory,
one per line (not the path, just the filename)
that contain the string "500".
When I ran:
ls -al
I got the following:
total 36
drwxr-xr-x. 2 501 dialout 4096 Feb 10 21:08 .
drwxr-xr-x. 39 501 dialout 4096 Apr 18 19:04 ..
-rw-r--r--. 1 501 dialout 204 Apr 29 17:44 README
lrwxrwxrwx. 1 501 dialout 23 Feb 10 20:59 access.log -> ../../common/access.log
lrwxrwxrwx. 1 501 dialout 25 Feb 10 21:08 access.log.1 -> ../../common/access.log.1
lrwxrwxrwx. 1 501 dialout 25 Feb 10 21:08 access.log.2 -> ../../common/access.log.2
I tried a few things, then looked at the user submitted solutions and one of them was:
ls *[^2]
I did some googling and the man page (and here), but I can't see what this is doing, or how it works.
Can anyone point me to a decent resource so I can read up on it, or tell me how it works?
Let me first quote PesaThes comment to what the command does:
The reference you are looking for is in the manual under: pattern matching. * matches any string, [^2] matches any character that is not 2. So the command lists all files that do not end in 2
Now why this is a solution to the problem is not so clear from your question alone. But if you look what the files contain you will notice that indeed, access.log.2 is the only one that does not contain the string 500 and also the only one whose name ends in 2.
For other sets of files the command ls *[^2] will most probably not output all the files without the string 500 in it, but in this case with those specific files it matches the right files. Another solution would have been for example
echo README; echo access.log; echo access.log.1
that's not an answer to your question, the right way of doing it is
$ grep -sl 500 * .*
-s skip errors (caused by directories); l only filenames; search in * all visible files and .* invisible files.

Pulling information from a who command on all servers [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 7 years ago.
Improve this question
I'm creating a program that pulls information from a who command, see also this question to see the format of the who command.
Now what I want to do is ssh to different servers and run the who command. Problem being I have no idea how to ssh in Ruby. I'm aware of require 'net/ssh/gateway' would somebody mind giving me an example of how I can ssh in Ruby and and perform a who command (like the linked question) on multiple servers?
For example:
def user
cmd = `who`.gsub(/[ \t].*/,"")
puts cmd
#<= Do some fancy stuff that will ssh to the servers and run cmd
end
Thank you ahead of time.
I've found out that doing something like this:
26 print "Enter password: "
27 system "stty -echo" #<= Removes echo from typing, you won't see your keystrokes
28 #password = gets.chomp
29 system "stty echo"
30
31 def logged_in
32 cmd = `who`.gsub(/[ \t].*/,"")
33 check = Net::SSH.start(#host, #username, :password => #password)
34 check.exec!(cmd)
35 end
36
37 #host = %w(servers).each do
38 logged_in
39 end
40
41 #username = Etc.getlogin
Will do what I want to accomplish.

Name of the users which executed a command [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to get the name of the users which executed a command (for example cat).
fc -l will provide a list with the most recent commands executed by the current user but is there an way to find out the history for all users?
I read the manual but i could not find something that would help
Do you know any other commands which would do this job?
I also tried w and who
I found this solution: the super user will search in each dir from "home" in the .bash_history and make a grep on that file for that command. It will work but is this optimal?
Using awk =)
awk -v monitoredcmd=cat '
$1~"^#[0-9]{10,}\s*$"{
sub(/#/,"")
tmpdate=$1
}
$1==monitoredcmd{
"date -d #"tmpdate | getline date
close("date -d #"tmpdate)
print "command [" $0 "] by",
gensub(/\/home\/([^\/]+).*/, "\\1", "", FILENAME),
at,
date
}
' /home/*/.bash_history
Sample Output
command [cat file.txt] by sputnick mer. févr. 13 15:34:44 CET 2013
command [cat l.py] by sputnick mer. févr. 13 15:45:38 CET 2013
command [cat foobar.pl] by marc mer. févr. 13 15:47:54 CET 2013

Resources