New answers tagged

0 votes

Getting the first file in a directory in bash

All existing answers seem to first create a full listing of the directory, which can take a while with thousands of elements, so I ended up with the simple: ls -AU | head -1 Where -U means unsorted ...
user avatar
  • 395
0 votes

Global search + replace, but only in cases where a test passes

Your description says that you have to test any single replacement and keep it or rollback. So you can do the modification in-place, test and in case of failure restore the backup file, do nothing in ...
user avatar
  • 6,130
0 votes

what is the zsh equivalent of bash's export -f

Everywhere I've read about this suggests that automatically importing functions is a bad idea, so I decided to just extract my scripts into a separate file that can be easily sourced in a subshell. I ...
user avatar
5 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

You can do it with a custom date format string that produces a shell arithmetic expression for the POSIX formula for seconds since the epoch: secs=$((`TZ=GMT0 date \ +"((%Y-1600)*365+(%Y-1600)/4-(...
user avatar
0 votes
Accepted

How can I batch rename files by combining three variables and ignore file extensions?

There are several problems in your shell script that lead to the errors you observe: You say that the first name part is to be CLSMFILE. However, in the variable assignment you state name1=$CLMSFILE, ...
user avatar
  • 18.4k
2 votes
Accepted

String compare negating in bash doesn't behave as it should be?

The problem here is that you have a PHP configuration file that you created on Windows. As a result its line ending are CR/LF rather than the UNIX standard of LF. You can prove this by modifying your ...
user avatar
  • 88.9k
-2 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

time=$(perl -e 'print time, "\n"')
user avatar
  • 797
0 votes
Accepted

Brute force bash scripting

A few issues I can see, a key problem is that you are executing the curl before the loop. I think this should work better, without diverging too much from your original code. Also you might need to ...
user avatar
  • 3,292
0 votes

why websocat or wscat in background with nohup, disown, bg, &, &! don't work

Hi found a python way to do it (so nohup, &, etc works) https://pypi.org/project/websocket-client/ pip3 install websocket-client in the .py from websocket import create_connection ws = ...
user avatar
  • 115
0 votes

Delete folders from a directory once it's sbatch`ed in a for loop?

If I understand correctly, what you want to do is: Submit one cellranger job to Slurm for each of your fastq directories. Delete the directory. However, that isn't what your script is doing. You ...
user avatar
  • 215k
21 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

For the epoch time as an integer number of seconds, that would be: awk 'BEGIN{srand(); print srand()}' or: awk 'BEGIN{print srand(srand())}' As in POSIX awk, srand() without argument uses the ...
user avatar
11 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

Write a C program that calls time() and prints the result. Borrowing from sample program in the specification of the time() function, let's call this e.g. seconds.c: #include <stdio.h> #include &...
user avatar
  • 115k
1 vote
Accepted

Filter rows of a huge csv file based on values in 2 columns

Awk works by splitting the line into fields which are then referred to by the field number and not by the text in the field. So you don't use $field1 for the first field, you use $1 and then $2 for ...
user avatar
  • 215k
2 votes

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

Many argument parsing libraries let you use an = sign to join a long option name and its value. (This includes Gnu libc's parsers and Python's standard argparse library.) If you're calling such a ...
user avatar
  • 591
2 votes

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

The Proper Way for storing arbitrary commands or arguments in a variable would be to use an array, see How can we run a command stored in a variable? But you can just tell shellcheck you like it just ...
user avatar
  • 115k
5 votes

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

For your specific use case, there's a much easier answer: unset -v area if __SOME_SETTING__; then area="ca,us" fi process_data -i /some/path ${area+ --area "$area" } If for ...
user avatar
2 votes

Define bash function and chain `script` with it in a shell script

To complement @terdon's answer, with Korn-like shells other than bash that support typeset -f (ksh, mksh, zsh), or with bash as well if only to avoid the pitfalls of the function export feature (see ...
user avatar
3 votes
Accepted

Define bash function and chain `script` with it in a shell script

The problem here is that your function only exists in your script, but then you run the script command which invokes a new shell (the one whose path is stored in $SHELL or /bin/sh otherwise). That new ...
user avatar
  • 215k
1 vote

Setting time zone in a kindle bash file

There was something going wrong in the kindle, no matter how much I tried to solve it or change the timezone. but what worked is adding this line MinuteOTheDay=$(expr \( $MinuteOTheDay + 400 \) % 2400)...
user avatar
-1 votes

Setting time zone in a kindle bash file

Use GMT0, and consider the difference between that timezone relative to you, which is -4hrs: MinuteOTheDay="$(env TZ=GMT0-4 date -R +"%H%M")"; Based on this answer: https://unix....
user avatar
13 votes
Accepted

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

Use arrays Here is your code re-written using arrays. Also to be a working example (ls in place of your command), and using correct case for variables (coding standard says that capitalised names are ...
user avatar
0 votes

How to save in a variable the convert from decimal to binary without use an echo

Note that if your shell is ksh or zsh, you don't need bc to convert to binary¹. in ksh/zsh: $ typeset -i2 num1=10 $ print -- "$num1" 2#1010 $ print -- "${num1#??}" 1010 With zsh: $...
user avatar
0 votes

gpg asks for password even with --passphrase

You can preset the passphrase with gpg-preset-passphrase (useful for --sign, --clearsign, etc.): apt install -y gpg gpg-agent mkdir -p ~/.gnupg echo allow-preset-passphrase| tee -a ~/.gnupg/gpg-...
user avatar
  • 121
3 votes

Displaying users logged in today between two dates

Use last command with -s and -t or -p flags: -s, --since <time> display the lines since the specified time -t, --until <time> display the lines until the specified time -p, --...
user avatar
  • 20.6k
1 vote

Rename files with numbers already in file name

With zsh: autoload -Uz zmv To autoload that batch renaming function (best in ~/.zshrc) zmv -n '(water-frames)(<0-999>)(.gro)' '$1-${(l[4][0])2}$3' Remove -n when happy.
user avatar
1 vote
Accepted

Rename files with numbers already in file name

Using the perl rename utility: $ rename -n 's/(\d+)(\.gro)$/sprintf "-%04i%s", $1, $2/e' ./*.gro rename(./water-frames0.gro, ./water-frames-0000.gro) rename(./water-frames116.gro, ./water-...
user avatar
  • 66.6k
4 votes

Remove files except latest one by numeric order

With zsh: rm -f -- *core*(n[1,-2]) Where the n glob qualifier turns on numericglobsort for that one glob expansion and [1,-2] selects the first to second last. Or: rm -f core-<1-4294967295>-xyz....
user avatar
5 votes
Accepted

Make script answer "Y" to prompts from apt-get

The option -y to apt-get will automatically answer "yes" to prompts. This also causes apt-get to run non-interactively. If a held package were to be changed, an unauthenticated package would ...
user avatar
  • 267k
-1 votes

Remove files except latest one by numeric order

Oh i found the solution. ls -v | grep core | tail -n 1 will do the trick. Reference: List files sorted numerically
user avatar
  • 119
0 votes

How to iterate a CSV file in bash?

Fields in a csv file could span several lines, for this reason and others, it's why I preferred to use xsv when I had to parse csv. One way to parse a csv file with bash and xsv can be : csvFile="...
user avatar
  • 11
1 vote

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

While @glennJackman's approach with a wrapper that checks if the first arguments starts with - and contains i is likely good enough in most common cases, there are a few cases where it fails: It gets ...
user avatar
0 votes
Accepted

What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?

With zsh, you could do: while IFS= read -ru3 url; do curl -- $url print -ru $(( $? ? 4 : 5 )) -- $url done 3< urls 4> bad 5> good That way, the bad and good files are opened only once ...
user avatar
1 vote

What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?

Using bash, you can loop for the urls and test the curl command, --fail option of curl seems to be good for use inside scripts, see: How to check whether a command such as curl completed without error ...
user avatar
  • 6,130
0 votes

Decoding URL encoding (percent encoding)

From my laymen research of the topic, it appears that the implementations of the percent-encoding are susceptible to ambiguity in edge cases, such as character encoding potentially being different ...
user avatar
0 votes

Trying to write a script to find ORF

Not clear what you're trying to do, but if it's about breaking the input into groups of 3 letters (codons?) and finding the sequences of those that start with ATG and end with the next occurrence ...
user avatar
0 votes

Command to toggle on/off touchpad in Xfce

I'm running Xubuntu 21.10 (xfce). The xinput method didn't work because synclient/synaptics wasn't in sync & is still running. Rather than try the synclient solution, which may not work in the ...
user avatar
0 votes

How to pass a parameter in a command without run the parameter content?

This: function denyCheck(){ is a mix of the standard function declaration func(), and ksh's function func. Bash supports the combination, other shells might not, but there's no reason to do it. Just ...
user avatar
  • 115k
1 vote
Accepted

extract tar then enter its directory (shell scripting)

A shell script can never change the working directory for its parent process (the shell invoking the script). So although you may change to the created directory in the script, as soon as the script ...
user avatar
  • 267k
0 votes
Accepted

How to pass a parameter in a command without run the parameter content?

Should rather be: cmd_canonical_path() { local cmd_path cmd_path=$(command -v -- "$1") && cmd_path=$(readlink -e -- "$cmd_path") && [ -x "$cmd_path&...
user avatar
3 votes

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

I'd use a wrapper function: you can add this to your .bashrc (untested) mv () { case $1 in -*i*) # ok, used `mv -i ...`: invoke the mv command, passing all args command mv &...
user avatar
1 vote

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

The easiest way is to create an alias. alias mv="mv -i" More complicated, but more customizable, would be to create an mv shell script, put it into the /bin, and the real mv move to /sbin ...
user avatar
  • 1,163
0 votes

Use config file for my shell script

Inspired by this article mentioned here in this response I came up with my own solution. The idea is the same, make a filtered copy of the config file in /tmp and use this one instead of the original. ...
user avatar
  • 111
1 vote

Prevent sourcing a bash script, in the script itself

Based on @roaima ' s answer: add this line early in your script. return 0 2>/dev/null || : It works even after set -e It will not show an error message when not using source. It prevents running ...
user avatar
  • 131
1 vote
Accepted

Periodically transfer files from two remote servers to single host using shell script

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the ...
user avatar
  • 88.9k
0 votes

looking to compare two different directories using the hash values of files within the directories

If you want to recursively include the contents of every file in the directory in the single hash, you can use find to find all normal files, and exec cat to concatenate them, and pipe that into ...
user avatar
0 votes
Accepted

looking to compare two different directories using the hash values of files within the directories

You can compare two directories using hash functions with commands like: cd dir1; sha256sum * >../hash_file cd ../dir2; sha256sum -c ../hash_file >/dev/null 2>&1 EXITCODE=$? Exit code 0 ...
user avatar
  • 12.9k
0 votes

looking to compare two directories using boolean statement

You can use diff command and get the boolean result by exit code: diff dir1/ dir2/ >/dev/null 2>&1 EXITCODE=$? if EXITCODE variable is 0 the directories are identical, if 1 they are not ...
user avatar
  • 12.9k
0 votes
Accepted

How can I read each line of a text file (contains unique id) in bash using for loop, save it into a variable and use it subsequently for

When declaring a variable, you aren't allowed to have spaces around the = sign, so your file variable isn't getting set. You also need to prefix it with a $ sign when you want to iterate through it. ...
user avatar
1 vote
Accepted

Extract field and number of occurrences per line

Try this: $ awk -v FS="', '" '{print $4 " |"NF-1 " " NR}' file If you have blank lines in file, you may want to use: $ awk -v FS="', '" 'NF >= 1 {print $4 &...
user avatar
  • 2,229
2 votes
Accepted

How to make libreoffice headless conversion act differently on file loading error

In an ideal world libreoffice will let you know with an exit code that the conversion did not take place. But it seems that's not the case. So you have at least two alternatives, one is parsing the ...
user avatar

Top 50 recent answers are included