Hot answers tagged

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
9 votes

Two ssh output as awk input

Beware of those curly quotes (“ and ”) some Windows text editors will use, use straight quotes instead (' or "). You should also be using 's, unless you have some reason to use "s, e.g. to ...
Ed Morton's user avatar
  • 30.3k
7 votes
Accepted

Two ssh output as awk input

You need to group the commands. The simplest way is to use curly braces which group them without creating a subshell: { ssh username@host1 'cat /tmp/test/*'; ssh username@host2 'cat /tmp/test/*'; } | ...
terdon's user avatar
  • 239k
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
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
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
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
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
  • 239k
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
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
  • 239k
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
4 votes
Accepted

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
  • 81
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,782
4 votes

How can I use parameter expansion together with command substitution?

Use ";" as the input field separator to split the output of locale man: IFS=';' mkdir -- $(locale mon)
StrangeAttractor's user avatar
3 votes

Two ssh output as awk input

One possible way is to exec ssh command in subshell and pipe the result on awk: (ssh username@host1 "cat /tmp/test/*"; ssh username@host2 "cat /tmp/test/*" ) | awk ‘ command ‘
Romeo Ninov's user avatar
  • 17.2k
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
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

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
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
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
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
2 votes

Bash - Check if the standard input contains anything

[[ ! -t 0 ]] = Does standard input contains anything? Your premise is wrong. This tests whether stdin is connected to a terminal/tty, not whether stdin contains anything. Because of the negation the ...
Chris Davies's user avatar
2 votes

make new directories/folders based on contents of column

I'm not sure what you mean by "contents of the folder" - are those contents directories or files of some sort? If directories this can be accomplished pretty easily with: < /tmp/input....
keithpjolley's user avatar
2 votes
Accepted

How can I use parameter expansion together with command substitution?

You can't use brace expansion like that, use xargs instead: locale mon | tr ';' '\n' | xargs mkdir
user000001's user avatar
  • 3,573
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
  • 46
1 vote
Accepted

Find file in directories of PATH env which match partially

This should do the work: compgen -c | grep 'YOUR_SEARCH_STRING' Example: compgen -c | grep '\-linux' Hope that helped :)
Pixelbog's user avatar
  • 667
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
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.5k
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
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

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