Questions tagged [shell-script]
Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.). Check scripts with https://shellcheck.net before posting.
16,787 questions
0
votes
0
answers
49
views
how to convert hex memory dump to Docsis config file?
Hex memory dump is Docsis config file. Is it possible to convert this hex dump info valid Docsis config file? Preferably, using Perl and DOCSIS::ConfigFile module. (or Python scripts, python-docsis). ...
2
votes
1
answer
197
views
Replacing the domain string in the compiled binary file with an IP address
Just in theory: is it possible to replace the domain string in the compiled binary file with an IP address by editing a binary file in place with sed? (IP address belongs to a different domain, but ...
11
votes
1
answer
1k
views
Is there any difference between [[ -n $1 ]] and [[ $1 ]] in bash?
The test [[ -n $1 ]] yields True if the length of string is non-zero.
But I've seen elsewhere and have tried using just [[ $1 ]] without the primary -n and it seems to have the same effect.
Is there ...
2
votes
1
answer
95
views
Bash: function in a script with flags
Using https://stackoverflow.com/a/7948533/31298396, we can implement flags for a script testing as follows:
#!/bin/bash
# Flags
# Source: https://stackoverflow.com/a/7948533/31298396
TEMP=$(getopt ...
5
votes
1
answer
343
views
zsh - if condition comparison: '-ne' and '!=' have different result
Why these commands lead to opposite comparison results between -ne and !=? Seems != works but -ne does not? I thought they are the same while comparing strings.
% set -o | grep pipe
pipefail ...
0
votes
1
answer
64
views
Bash Script Linux - combines a QUIET function with other functions without hiding some of them
#!/bin/bash
DefaultColor="\033[0m"
GREEN="\033[32m"
RED="\033[31m"
PURPLE="\033[35m"
YELLOW="\033[33m"
CYAN="\033[36m"
CmdOk="\033[...
5
votes
1
answer
373
views
locking scripts using flock - can same file descriptor be used concurrently?
Common method for preventing concurrent runs for a given script is utilizing flock as:
LOCK=/var/lock/my-lock
exec 9>>"$LOCK"
if ! flock --exclusive --nonblock 9; then
echo "...
1
vote
1
answer
57
views
Docker Container Mount Name, Type and other values
I am writing a bash script and I am aware that I can get information of the mounts attached to a container using docker inspect --format '{{.Mounts}}' <container-name> which gives a result such ...
6
votes
2
answers
283
views
Change some file names during a backup with rsync
I use a simple backup bash script to copy some directories originally placed in a Debian (Testing) from a USB stick (formatted NTFS) to a MacBook. The names of directories are the same from origin to ...
10
votes
4
answers
943
views
With `#!/bin/sh`, what is the closest to `;;&` (`bash`'s fallthrough)?
Without ;;&, /bin/sh gives Syntax error: ")" unexpected (expecting ";;").
;;& triggers shellcheck's ^-- SC2127 (error): To use cases with ;;&, specify #!/usr/bin/env ...
0
votes
0
answers
75
views
A bash script problem installing Apache Cloudberry - it's a generic setup script - just isn't working for me
This is a fairly bog-standard script, but my bash knowledge isn't huge, so here goes.
The script is from here - the Apache Cloudberry project - an interesting open source version of a distributed ...
-2
votes
1
answer
128
views
extract fields of a string separated by |
In the below shell script I want to extract the fields of a string delimited by | character. But unfortunately I'm facing issue if there exists pipe command | in the string, because of which I'm not ...
0
votes
5
answers
867
views
Extract email addresses from line, with multiple email addresses per line
I have a text file that mixes names and email addresses, in a comma-separated list.
First Person <[email protected]>, Second Person <[email protected]>, Third <[email protected]>
...
-3
votes
3
answers
141
views
How to pass an array as an argument to a function
In the below shell script I am passing an integer and a string array to a print function which should print each parameter separately, but I'm not able to. I don't want make a reference of passed ...
1
vote
2
answers
128
views
How to pass an argument by reference to a function in KornShell (PDKSH 5.2.14)
In bash I can use
local -n ref_name=$1
to pass an argument to a function by reference, but the same syntax in ksh throws an error saying
typeset: -n: unknown option
I tried nameref ref_name=$1 ...
1
vote
1
answer
80
views
List all the directories residing in a directory path using KornShell
I have the below shell script which works fine in Bash to list all the directories residing inside a given directory path and add them to an array. But the same script code doesn't work in KornShell.
...
0
votes
0
answers
84
views
Why mount a directory to itself. mark it make-rshared and mount option inside a container
I’m using BTRFS snapshots for my backups with Restic, and with the help of GenAI I created a script to automate the process. I take snapshots of a subvolume called home, which contains three ...
4
votes
4
answers
839
views
How to check if a path exists from a string with case insensitive?
Hello i try to check if a path exists with a if statetment in a bash script from a string. I try the follow but nothing seems to handle this.
The path is /home/orhan/Blog/Mai1
path='/home/orhan/blog/...
4
votes
4
answers
546
views
How to wait for background commands that were executed within a subshell?
Given this code:
#!/bin/bash
set -euo pipefail
function someFn() {
local input_string="$1"
echo "$input_string start"
sleep 3
echo "$input_string end"
}
function ...
10
votes
7
answers
1k
views
Check if multiple files exist on a remote server
I am writing a script that locates a special type of file on my system and I want to check if those files are also present on a remote machine.
So to test a single file I use:
ssh -T user@host [[ -f /...
2
votes
1
answer
170
views
Semicolon in conditional structures after the closing double bracket in a bash/zsh script?
Continuing Semicolon in conditional structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]?
In my tests, running
#!/bin/zsh --
if [[ &...
1
vote
0
answers
68
views
How to send an email with an attachment, but so that the attachment is not saved on the mail server
I send emails from the terminal with mutt -s "This is Subject" -a a.txt -- [email protected] < test.txt. The a.txt attachments can be large, how can I avoid filling up the mail server?
...
5
votes
2
answers
846
views
What does VAR=() mean in a script?
In the file /usr/bin/gcore (which is a bash script on my system), there are the following lines:
# When the -a option is present, this may hold additional commands
# to ensure gdb dumps all mappings (...
0
votes
5
answers
213
views
Rename a set of files according to a specific scheme, with rename back option
In Linux in a directory there are files, ls -1 shows me this output :
file1.1-rvr
file1.2-rvr
file1.3
file1.4-rvr
file1.5
file1.6-rvr
file2.1
file2.2
file3.1
file3.10
file3.2-rvr
file3.3-rvr
file3.4
...
6
votes
1
answer
265
views
bash & zsh friendly process substitution
In bash I'm using this common pattern where null-terminated list of process output is processed in a loop, e.g.
declare -a commits
while IFS= read -r -d $'\0' c; do
commits+=("${c%% *}")
...