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>...
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 ...
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/*'; } |
...
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.
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
...
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 ...
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
...
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 ...
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 ...
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" | ...
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 '*....
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 ...
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 ...
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)
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 ‘
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 ‘...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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....
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
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, ...
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 :)
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell × 11996bash × 4110
shell-script × 2388
linux × 1430
scripting × 728
command-line × 685
zsh × 518
quoting × 498
awk × 479
terminal × 446
sed × 423
ssh × 381
files × 375
text-processing × 374
io-redirection × 371
find × 366
grep × 347
pipe × 330
wildcards × 308
environment-variables × 270
variable × 246
ubuntu × 213
posix × 190
process × 185
filenames × 172