47 votes

How to run a script every 64 hours?

Just run every hour and check that the number of hours since some arbitrary instant (for instance, instant 0 of the Unix epoch time) is a multiple of 64: 0 * * * * t=$(date +\%s); [ "$(( (t / ...
user avatar
27 votes
Accepted

How to run a script every 64 hours?

I suggest perhaps using a crontab "front end" like crontab.guru for figuring out crontab if you're a beginner. However, as in your case, the hour setting only allows for values of 0 to 23, ...
user avatar
  • 10.5k
26 votes
Accepted

Using wildcards to elegantly convert thousands of epub files to mobi

You can’t do it directly with wildcards, but a for loop can get you there: for epub in ./*.epub; do ebook-convert "${epub}" "${epub%.epub}.mobi"; done Zsh supports a more elegant ...
user avatar
26 votes

How to run a script every 64 hours?

cron is made to run things on date or clock events, i.e. every 1st of a month or week or every third hour of a day, respectively. For running things in time intervals that cannot be simply expressed ...
user avatar
  • 11.2k
21 votes
Accepted

Run script without pressing enter

You can do this using bash's .inputrc file, the readline startup configuration file. First, edit the file ~/.inputrc (this means a file named .inputrc in your $HOME directory; create it if it doesn't ...
user avatar
  • 219k
21 votes
Accepted

How can I shorten HH:MM:SS as much as possible?

As I understand you need to remove the leading zeroes, so let's suppose you have a file like this 00:00:30 00:01:30 00:30:00 01:30:00 30:00:00 would become 30 1:30 30:00 1:30:00 30:00:00 If that's ...
user avatar
17 votes

Why does using -F flag work while explicitly changing field separator by using FS variable not work when field seperator to be changed is '\t' in awk?

You can't use ' within a '-delimited string (including a script) in Bourne-like shells such as bash. The string delimiter inside an awk script is ", not '. Just change FS='\t' to FS="\t&...
user avatar
  • 23.6k
17 votes

What are the benefits of this command line (/usr/bin/awk '{$1=$1};1') instead of pure cat

key=$(/bin/cat "${file}") sets key to the content of the file¹, without its trailing newlines. (I'm assuming a text file, without null bytes.) key=$(/usr/bin/awk '{$1=$1};1' < "${...
user avatar
15 votes

while command not working when bash script is sourced in tcsh?

This error? $ tcsh tcsh> source while.sh i=1: Command not found. while: Expression Syntax. tcsh> exit Csh/tcsh is a different shell than POSIX sh or Bash. Trying to run a script in sh syntax ...
user avatar
  • 120k
12 votes
Accepted

What are the benefits of this command line (/usr/bin/awk '{$1=$1};1') instead of pure cat

The assignment to $1 forces awk to rewrite the input line to a canonical format. { echo 'one two three'; echo ' indented with trailing '; } | cat one two three indented with trailing { echo '...
user avatar
  • 91.9k
12 votes
Accepted

Use xargs and kill in script with optional signal

The problem is that you’re explicitly adding a space to SIGNAL: SIGNAL=" -${SIGNAL}" and then referencing it in quotes:kill "$SIGNAL" Since kill is seeing an argument that doesn’t ...
user avatar
11 votes

How to print to stdout after exec >/dev/null

Any echo would print to stdout. It's just that your stdout now points to /dev/null. Point being that the original stdout is in no way special, or more "true" than the stdout you have after a ...
user avatar
  • 120k
10 votes
Accepted

if ! <command> (...) vs. <command> ; if [ $? -eq 0 ] (...)

You only need to use the special variable $? when you need its value across several invocations of other utilities. For example, you may want to output it in a diagnostic message and then return it ...
user avatar
  • 13k
8 votes

Using wildcards to elegantly convert thousands of epub files to mobi

With the zsh shell: for f (./*.epub) ebook-convert $f $f:r.mobi With $f:r expanding to the rootname (the part without the extension) of $f like in csh/vim... Or: autoload -Uz zmv # best in ~/.zshrc ...
user avatar
7 votes
Accepted

How to use getopts in bash

getopts only supports single-character option names, and supports clustering: -sp is equivalent to passing -s and -p separately if -s doesn't take an argument, and it's the option -s with the argument ...
user avatar
6 votes

Using wildcards to elegantly convert thousands of epub files to mobi

Can you use wildcards in a similar way, that works for a scenario like the above? Not like ebook-convert *epub *mobi, because of how wildcards - really "shell globbing" - works. But, you can ...
user avatar
  • 161
6 votes

How to run a script every 64 hours?

64 hours is 2 and 2/3 days, so 3 * 64 hours = 192 hours = 8 days. So you could almost do 0 0 1-25/8 * * foo 0 16 3-27/8 * * foo 0 8 6-30/8 * * foo except that you get a short pattern at the end of ...
user avatar
  • 675
6 votes
Accepted

Is it possible to perform a command on string stored inside a variable?

The cut utility reads data from its standard input stream, it does not operate on strings given as arguments. To use cut, therefore, you need to pass the data on standard input: YYYY=$( printf '%s\n' &...
user avatar
  • 13k
6 votes

Strings comparing issue on bash script

I strongly suggest you rather use the is-active query from systemctl systemctl is-active gravity which returns the current service status, or: sytemctl --quiet is-active gravity which just exits ...
user avatar
  • 11.2k
6 votes

Unix Delete Files and Folders older than 30 days but skip one specific directory and the files/folders nested inside

You appear to have the logic of your find command mixed up. If you want to exclude a directory named excludeme and otherwise print filenames, you would write: find /path/to/top/directory -name ...
user avatar
  • 26.1k
6 votes

How to print to stdout after exec >/dev/null

If you want the echo output to go specifically to the terminal the script was started from, you could use: echo "some message" > /dev/tty /dev/tty is slightly "magic": ...
user avatar
  • 71k
5 votes

ADD or SUB numbers based on op-code (directive) in the line

Some tips: use either space or comma as the field separator: FS = "[ ,]" then add up the numbers like you do, except start with i = 2 if the first field is SUB, multiply the sum by -1
5 votes
Accepted

Linux Shell Script: how call a method and do the evaluation of the returned value within an if statement at the same time

if command succeeds if command exits with a 0 status; in your case, if verifyIfFileExists; then ... else ... fi [ and [[ are themselves commands which return 0 or 1 depending on the result of ...
user avatar
5 votes

Stop command in ssh session without exiting ssh

I got same behavior after installed Git for Windows 2.35.1.2. I can avoid it after downgrade to version 2.34.1 from here.
user avatar
4 votes
Accepted

'If' statement breaking his own rule or I'm doing so

This line: sendnot=$(echo ... | mail ... < <(./ssl-cert-check ...)) runs the commands echo, mail and ./ssl-cert-check right then and there, and captures the output of the whole lot, i.e. the ...
user avatar
  • 120k
4 votes

find larger files under /tmp (owned by oracle user, size larger than 1 MiB) and truncate them

To bring their file size down to 0: find /tmp/ -size +1048576c -user oracle -type f -exec truncate -s0 {} + If your system doesn't have a truncate command: find /tmp/ -size +1048576c -user oracle -...
user avatar
4 votes
Accepted

What do the commands below mean? x=${1//[^ 0-9]/} and ${#x}

x=${1//[^ 0-9]/} $1 is the first positional parameter / command line argument. ${var//pattern/replace} takes the value of $var and replaces all occurrences of pattern with replace. Here, the pattern ...
user avatar
  • 120k
4 votes
Accepted

Using a shell script to parse through a CSV file and add users using the ldapadd command

ldapadd expects dn: ... from standard input. You need a "Here document": #!/bin/bash while IFS=, read -r userName firstName lastName gender dob language bloodType zodiac constellation planet ...
user avatar
  • 1,811
4 votes
Accepted

Every new loop has a different parameter value

It looks like what you want is something like: for i in 75 80 85 90; do cwebp -q "$i" file.jpeg -o file.webp; done Though this will overwrite the output file each time, so you might want to ...
user avatar
  • 65.1k
4 votes

echoing value in same indexes of 2 arrays simulataneously

Just use a numerical index and reference the array elements by their index: labels=(label1 label2 label3 label4) values=(91 18 7 4) for((i=0; i<"${#labels[@]}"; i++ )); do printf '%s: %...
user avatar
  • 219k

Only top scored, non community-wiki answers of a minimum length are eligible