New answers tagged

0 votes

How to run all scripts of the form `sh my_dir/subdir*/qsub.sh`

As bxm already said: The shell is expanding your command to sh my_dir/subdirA/qsub.sh my_dir/subdirB/qsub.sh my_dir/subdirC/qsub.sh which runs my_dir/subdirA/qsub.s with the arguments my_dir/subdirB/...
entropie's user avatar
1 vote
Accepted

How to disable "long descriptions" of commands in ZSH

A lot of aspects of completion can be configured through zstyle. If the completion code for fhome follows the usual principles: zstyle ':completion:*:*:fhome:*:*' verbose no There may be ways to ...
Gilles 'SO- stop being evil''s user avatar
4 votes
Accepted

How to run all scripts of the form `sh my_dir/subdir*/qsub.sh`

The reason your command does not work is because the shell will execute the first match, and the rest of the matches will be passed as parameters to that first script. You need to iterate the ...
bxm's user avatar
  • 4,776
1 vote

How can I get a file list, recursively?

With globstar and extglob enabled, you can recursively find filenames that match a certain pattern. shopt -s globstar extglob For example, to print all files under media/ that end with either mp3, ...
dbran's user avatar
  • 36
2 votes

How to run the command "sudo sh /path/to/script.sh" by double clicking?

EDIT Since you edited your comment to provide more information, I can now give you a complete answer. I verified with my friend who runs KDE plasma, and he says that the default behavior is that ...
renzev's user avatar
  • 46
4 votes
Accepted

How can I get a file list, recursively?

If you mean you want the full paths of the regular files whose name ends in .lua in or under the /media/username directory, then you could use the standard find. LC_ALL=C find /media/username -name '*....
Stéphane Chazelas's user avatar
6 votes
Accepted

Is there a away to override a shell's non-interactive state?

Try expect. It is designed specifically to automate tasks over interactive stream.
White Owl's user avatar
  • 4,724
4 votes
Accepted

multiple commands in linux shell

That's a typo. The correct commands are: curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | ...
terdon's user avatar
  • 238k
3 votes
Accepted

How does the `tr` command work?

tr’s arguments are maps of characters: characters are mapped 1:1 in order. In the set of replacements, the last character is repeated as necessary; extra characters are ignored. Thus tr 5 x replaces ‘...
Stephen Kitt's user avatar
1 vote

How does the `tr` command work?

IMO this behavior is explained perfectly in the macos tr man page: In the first synopsis form, the characters in string1 are translated into the characters in string2 where the first character in ...
jesse_b's user avatar
  • 36.4k
2 votes

How to execute a subshell directly

A workaround in Bash may be to define a function, export it, finally use timeout 25 bash -c to run the function. This is even less "directly", but at least syntax highlighting should work. ...
Kamil Maciorowski's user avatar
0 votes

How to referencing $@ without pass it in bash function?

Based on the comments, OP is asking about how to imitate the behaviors of builtins such as shift and getopts in a function. It's actually possible with some tricks, though I'm not sure it's a good ...
user23013's user avatar
  • 1,077
5 votes

How to referencing $@ without pass it in bash function?

Actually, in bash, when the extdebug option is enabled (normally used for the debugger), the list of positional parameters of all the functions in the call stack is made available in the $BASH_ARGV ...
Stéphane Chazelas's user avatar
0 votes

How to check Windows remote path exists from Linux command?

@FelixJN answer did not work for me, smbclient with \\server\path\to\dir connects to \\server\path, not \\server\path\to\dir then always success if at least \\server\path exists, that said, inspired ...
gluttony's user avatar
  • 161
14 votes
Accepted

How to referencing $@ without pass it in bash function?

You can't without A making its positional parameters available to B one way or another. Could be by passing them along: A() { B "$@" } B() { [ "$#" -eq 0 ] || printf '<%s>...
Stéphane Chazelas's user avatar
4 votes
Accepted

How bash getopts knows what arguments the call has

getopts is a shell built-in, so it can reference $@ directly. It also sets the shell variables OPTARG and OPTIND. (Note that inside a function, getopts will reference that function's $@ rather than ...
Chris Davies's user avatar
0 votes

How to sort the list of positional parameters in POSIX sh

Here's an idea: In sh, escape all newline and backslash characters in each positional parameter, so that we could use the newline character as delimiter. Pipe the newline-separated positional ...
Flux's user avatar
  • 2,770
1 vote
Accepted

Grep text between two patterns on each line including the pattern

Using any awk: $ awk '$3=="installed"{ printf "%s%s", sep, $4; sep=OFS } END{ print "" }' file data data-cleo python
Ed Morton's user avatar
  • 30k
1 vote
Accepted

How to convert all newlines to "\n" in POSIX sh strings

Try awk with: string='x y ' new_string=$( LC_ALL=C awk -- ' BEGIN { gsub("\n", "\\n", ARGV[1]) printf "%s", ARGV[1] }' "$string" ) In any ...
Stéphane Chazelas's user avatar
3 votes

How to sort the list of positional parameters in POSIX sh

Probably easiest is to resort to awk which can do strcoll(), strcmp(), and number comparisons (including of floating points). To avoid reinventing the wheel, we can use the quicksort awk ...
Stéphane Chazelas's user avatar
2 votes

How to sort the list of positional parameters in POSIX sh

you need to use the shell to eval these string first, and then can sort the results, applying the same operations to the strings that get sorted, and the array of original indices. I'll illustrate ...
Marcus Müller's user avatar
1 vote

Key binding to swap shell foreground/background jobs

This makes issuing CTRL+Z immediately resume the previous job, as long as you have more than 1 job suspended. The only way to exit the while loop is to close programs until at most 1 backgrounded ...
Sakari Cajanus's user avatar
1 vote

Multiple Command List After Shell Conditionals, &&, ||. Shell Script, Dash

Several options, none of them is really appropriate ${VAR:?"Error msg"} eval "${VAR:-echo Error msg; exit}" Arithmetic evaluation supports ternary expression, but only for values. ...
Dani_l's user avatar
  • 4,845
0 votes

Multiple Command List After Shell Conditionals, &&, ||. Shell Script, Dash

I can't help but think you are actually looking for something you have said wasn't it: [[ -z "$temp" ]] && { echo nope; continue; }
BETLOG's user avatar
  • 1
6 votes

ARGV[] not accepting the argument

Sounds like you want something like: #! /bin/sh - DT=$(date -d yesterday +%m%d%Y) || exit export DT exec perl -lne ' if ( ($status, $machine) = /STATUS:\s+(\w+).+MACHINE:\s+(\w+\.\w+\.\w+)$/ and ...
Stéphane Chazelas's user avatar
4 votes

ARGV[] not accepting the argument

The reason this is failing is first because you are exiting the single quotes: perl -ne '[...] '$ARGV[1]', [...]' So your $ARGV[1] is being seen by the shell and not perl. Next, you don't actually ...
terdon's user avatar
  • 238k
5 votes

ARGV[] not accepting the argument

One issue that jumps out at me is in the second perl line: perl -ne '/^\s+(\d+)\s+(.*)$/ && print join("\t", '$ARGV[1]', $ENV{AUTOSERV}, $2, $1) . "\n"' $date_YYYYMMDD ...
Chris Davies's user avatar
2 votes
Accepted

Set PATH for /bin/sh in Debian:11 image

For /bin/sh in Debian (dash), /etc/profile and so on are only used for login shells. For non-login shells, you can specify a startup script to use in the ENV environment variable. At the end of your ...
Stephen Kitt's user avatar
0 votes

How to check if a pipe is empty and run a command on the data if it isn't?

Is more difficult to be achieved on C Shell, but I made it. #!/bin/tcsh -f while 1 head -c1 | sed p | \ ( exit ( ! `head -c1 | wc -c` ) && \ tail -c1 ) || exit end Also achievable on ...
Matheus Garcia's user avatar
1 vote
Accepted

How to decode base64-encoded inline attachments in email files saved from Thunderbird or Gmail?

The command base64 performs encoding by wrapping text at default column width of 76 characters, unless another value is specified with the option --wrap=COLS. The command base64 --decode is expected ...
Sadi's user avatar
  • 475
0 votes

atd reports Permission denied

I was searching for answers on why atd was running but jobs failed to executed with the following in the journal: Nov 16 05:28:00 yourserver atd[15038]: Cannot create output file: Permission denied ...
Kyle's user avatar
  • 111
2 votes

sh split value into variables

For dash (on Ubuntu) you can use traditional heredoc (<<) instead of herestring: read -r local_temperature system_mode ... <<END # END must be unquoted $(thermostat) END But for busybox I ...
dave_thompson_085's user avatar
0 votes

How to mark things I download automatically executable?

For your particular use-case, where you are cyclically downloading from the same URL and running what are actually different versions, perhaps something like this: while printf 'Press enter to ...
Martin Kealey's user avatar
0 votes

How do I print out the file size in a shell script?

In Ruby you can use: File.stat("/path/to/file").size
dinoex's user avatar
  • 26
1 vote

Three ways to get machine architecture

$ cat /proc/cpuinfo $ lscpu These give similar info in different formats and include architecture details like 32b mode memory address size byte order, specific cpu model and the available instruction-...
Max Power's user avatar
  • 264
0 votes

check patterns that don't exist in sqlite

Import your IDs into some new table, then use that table to query your main table. Drop the table whet you're done with it. { echo id; grep '^[0-9]' keys; } | sqlite3 database.db \ 'CREATE TABLE ...
Kusalananda's user avatar
  • 327k
3 votes

Add a process to an already executing process? Like && or so

Only the parent process (or the child subreaper if any or init if the parent died), can retrieve the exit status of a process. From the shell where you started the command, you can wait for it, and ...
Stéphane Chazelas's user avatar
0 votes

How to convert date to timestamp

On BSD OS (FreeBSD, OpenBSD, NetBSD), date doesn't support -d flag. Use instead -j flag ("Parse the provided date and time and display the result without changing the clock") and strftime ...
lcheylus's user avatar
  • 151
3 votes

Add a process to an already executing process? Like && or so

If the shell that started the process still knows about it (try jobs), then the shell built in wait can wait for it to finish. Something like this might work: $ jobs [1] nohup a.sh $ wait -f %1 &&...
user10489's user avatar
  • 6,213
1 vote
Accepted

A && B || C vs if then else fi

It is very easy to make a mistake in cases like A && B || C. They are almost universally "write only" - once you wrote such compound statement and it works - it is difficult to ...
White Owl's user avatar
  • 4,724
3 votes

defining variables inside function

FILE_CORE_NAME="$(/usr/bin/env basename $(/usr/bin/env awk -F "." '{print $1}' <<< "${FILE_TO_PROCESS}"))"; local FILE_CORE_NAME; Even when reformatted on ...
Stéphane Chazelas's user avatar
2 votes

defining variables inside function

local name=value creates a local variable and set it to value. In your case there is no value so you create a local variable which is empty and the local variable hides the non-local variable with the ...
Paul Pazderski's user avatar
0 votes

Calculate time difference between two dates

Astonished that no-one said to use Python! For example, 5 days ago as yyyymmdd: MYDATE=$( python -c 'from datetime import date, timedelta; print( ( date.today() - timedelta(days=5) ).strftime(&...
nigel222's user avatar
  • 317
0 votes
Accepted

File backed, key value store implemented with posix utilities

For alphanumeric key-values: kvfile="kvfile" put() { if grep -q "^${1}=" "$kvfile"; then sed -i "s/^${1}=.*$/${1}=${2}/" "$kvfile" ...
Tom Huntington's user avatar
3 votes

Identifying Linux distribution from screenshot

Does anybody know what distro this might be running? You can try these commands until one gives you a meaningful answer: cat /etc/debian_version cat /etc/fedora-release cat /etc/redhat-release cat ...
dr_'s user avatar
  • 29.1k
0 votes
Accepted

How to pass the standard input of a shell script to a background command

Yes, that's an annoying requirement of the POSIX sh specification that commands started asynchronously by non-interative shell invocations (when the monitor option IOW job control is not enabled) have ...
Stéphane Chazelas's user avatar
0 votes

How to rename a file into a random GUID?

On Linux: (ret=0 for file in *; do IFS= read -r uuid < /proc/sys/kernel/random/uuid || exit mv -i -- "$file" "$uuid" || ret=$? done exit "$ret")
Stéphane Chazelas's user avatar
0 votes

For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)

Use pdsh for multiple hosts.. It is widely used in data centers with large number of clustered hosts.. From pdsh documentation .. Run with user 'foo' on hosts h0,h1,h2, and user 'bar' on hosts h3,h5: ...
Abdullah's user avatar
  • 101
1 vote

For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)

A general solution for looping through two lists, using a pair of elements (one from either list) in each iteration of the loop, in a POSIX shell: #!/bin/sh # The list of port numbers set -- 123245 ...
Kusalananda's user avatar
  • 327k

Top 50 recent answers are included