All Questions
Tagged with bash-script or shell-script
16,764 questions
5
votes
2
answers
828
views
POSIX sh alternative to using [[ ... ]] in Bash
I am using this code to parse the first argument passed to my script. It error handles and it works just the way I want it:
if [ -z "$action" ]; then
printf "[${c_RED}ERROR${c_RESET}...
0
votes
1
answer
52
views
Reading data from serial port on linux [closed]
We have an old setup of a PLC and an old PC (from 90s). The PC previously ran a windows program reading PLC data from a Serial port and plotting it on the monitor. Long story short, the old program no ...
-1
votes
0
answers
65
views
Arch Linux start script
Hi I was just wondering if this script looks ok I run it after I login thanks.
#!/bin/bash
echo "hi, starting wifi, nft and desktop"
sudo ls
sudo systemctl start wpaStart.service
sudo nft -...
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
202
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 ...
1
vote
2
answers
64
views
Sorting output from find more like tree --dirsfirst -F
Here is a sample directory tree as it would appear if it were sorted in character code order (i.e., directories are not listed first):
${PREFIX}/
.bashrc
.include.sh.d/
common.sh
...
0
votes
0
answers
57
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). ...
0
votes
4
answers
80
views
Mount a partition from a script unless it is already mounted
I would like to mount a partition of an external hard drive from a script using
udisksctl mount --block-device /dev/sda1
and in the end of the script I would like to unmount it.
However, it may ...
5
votes
1
answer
349
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
59
views
LF file manager : how to go back to $OLDPWD?
What do I want :
On the file manager lf, we can bind some keyboard shortcuts to do either a lf command with :, or a shell command with $, !, or other symbols.
According to the documentation, there is ...
1
vote
1
answer
73
views
Bash: only the first long option is being processed
Suppose we have the file ./testing with contents
#!/bin/bash
# Flags
# Source: https://stackoverflow.com/a/7948533/31298396
TEMP=$(getopt -o ''\
--long first,second \
-...
2
votes
1
answer
102
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
391
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 "...
0
votes
1
answer
69
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[...
10
votes
4
answers
947
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 ...
1
vote
2
answers
56
views
Parent shell script blocked from exiting by background subshell
I have the following script flow:
The parent script is launched from a non-interactive shell and sources a child script.
child launches a background subshell with (...) & and contains no code ...
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 /...
0
votes
5
answers
881
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]>
...
13
votes
1
answer
2k
views
Shebang apparently calls wrong shell
I am currently relearning a few basics about shell scripts and encountered the following problem.
I have the following script error.sh
#!/usr/bin/env bash
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; ...
0
votes
0
answers
53
views
Sending SIGSTOP to background sleep process does not stop it; it keeps running
I am trying to implement a shell(zsh) based Pomodoro clock and I encountered a problem: sending -STOP signal to background sleep process does not stop it.
WesternGun@MyMacBook-Pro:~ [12:13:49] 0
% ...
4
votes
4
answers
853
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/...
6
votes
2
answers
286
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 ...
4
votes
4
answers
551
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 ...
5
votes
4
answers
525
views
Creating n folders with equal number of files from a large folder with all files
I have a large folder of data files, which I want to copy into subfolders to make a specified number of batches.
Right now I count how many files there are and use that to make ranges, like so:
cd /...
1
vote
2
answers
346
views
get process id of a process running in remote machine
In the below shell script, I am trying to get the process id of a remote process using ps command, but not getting the required output
#!/bin/bash
get_process_id() {
local res
local ...
2
votes
1
answer
201
views
`printf` and `time` formatting issues in a `zsh` shell script
I have the following (excerpt from a) zsh shell script (the FOLDER environment variable is exported earlier in the code):
# Create temporary directory
export TMPDIR=$(mktemp -d)
# Set TIMEFMT to ...
2
votes
4
answers
147
views
Is it possible to silence the initial call in xtrace, when BASH_XTRACEFD is set?
#!/usr/bin/env bash
exec {BASH_XTRACEFD}>./xtrace.log
declare -p BASH_XTRACEFD
set -x
{ : "how do you hide this in ./xtrace.log?"; } 2>/dev/null # fail
# { :; } "${BASH_XTRACEFD:-...
13
votes
7
answers
4k
views
Idiomatic way of generating a unique filename?
In a script I'm writing, I want to create something temporary on my filesystem, but - it's not in /tmp but elsewhere, and may not be a file nor a directory (e.g. maybe it's a named pipe or a symbolic ...
-4
votes
1
answer
228
views
How to redirect command output to a file if the command itself contains redirection
In the below shell script I'm executing the some command cmd remotely where in the command output is redirected to some file cmd_log. I'm getting the desired result as long as the command cmd itself ...
2
votes
2
answers
390
views
Can I execute multiple case blocks if the pattern matches?
I want to know if there is another alternative in Bash to execute multiple blocks when the pattern matches.
I don’t want to use another command, if statements, or two or more separate case blocks or ...
1
vote
1
answer
59
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 ...
-2
votes
3
answers
161
views
Generate hashes automatically and continuously with a script and stop by pressing a key?
I need a part for a script in bash where hashes are generated automaticaly and continuously and when I press a key the generation stops and the last hash at which I stopped is displayed.
I've cobbled ...
0
votes
2
answers
88
views
What explains the measured overhead when timing a command in zsh?
When timing a command with time, there is inevitably some overhead (from the CPU, operating system, shell and so on).
Running the following code, from a tty text mode virtual console with all ...
1
vote
2
answers
246
views
Check if a given function name exists in a sourced shell script file
In Bash I can use $(type -t function_name) = "function" to check if the function by name function_name exists or not in a sourced file which is sourced using source file_name command. I'm ...
422
votes
22
answers
852k
views
How do I trim leading and trailing whitespace from each line of some output?
I would like to remove all leading and trailing spaces and tabs from each line in an output.
Is there a simple tool like trim I could pipe my output into?
Example file:
test space at back
test ...
-3
votes
3
answers
146
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 ...
-2
votes
1
answer
132
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 ...
353
votes
27
answers
496k
views
How can I get my external IP address in a shell script?
I need to find my external IP address from a shell script. At the moment I use this function:
myip () {
lwp-request -o text checkip.dyndns.org | awk '{ print $NF }'
}
But it relies on perl-libwww,...
-2
votes
2
answers
102
views
HISTTIMEFORMAT not working as desired in RHEL 8 bash 4.4.20
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing:
Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-...
5
votes
2
answers
848
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 (...
466
votes
19
answers
713k
views
How do I change the extension of multiple files?
I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble changing more than one file.
Here's my code:
files=`ls -1 *.txt`
for x in $...
1
vote
1
answer
173
views
Execute a script at startup as a root that executes a software an keep it running
If executed manually with sudo, the script does its job.
#!/bin/bash
echo "Script executed at $(date)" >> /var/log/realtimesync.log
echo
/opt/FreeFileSync/RealTimeSync /mnt/harddrive/...
-4
votes
2
answers
303
views
Linux kill command returns "No such process"
In the below shell script I have list of process IDs which need to be killed. When killing a process I am getting kill: 1234567: no such process error even after checking if the process id existence ...
1
vote
2
answers
130
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.
...
14
votes
1
answer
3k
views
Why does cd '' succeed in bash?
Maybe I'm missing it, but I don't find it documented that cd '' should succeed. Since there is no directory with the name '', it seems obvious that it should fail. For example,
mydir=
cd -- "$...
5
votes
3
answers
548
views
Can grep output the complete input verbatim only if there was a match?
I'm processing input data in a pipeline consisting of several programs. In one step of the pipeline, I want to know if there are certain lines in the input, but I want to output everything as-is ...
6
votes
3
answers
903
views
How to read a line from a file and output the line to a file
A file like this: ExampleFile.txt
line1TextHere
line2TextHere
line3TextHere
I would like to read a line one by one from the file and output it to different files, so there will be one file for each ...
0
votes
0
answers
77
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 ...
682
votes
11
answers
307k
views
Why is it better to use "#!/usr/bin/env NAME" instead of "#!/path/to/NAME" as my shebang?
I notice that some scripts which I have acquired from others have the shebang #!/path/to/NAME while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME.
Both seem to work properly....