All Questions
Tagged with bash-script or shell-script
16,788 questions
3
votes
2
answers
154
views
print_one_line_and_wait_long_time | head -n1: early exit of pipe/process substitution [duplicate]
tl;dr: How to reliably ensure that the pipe a | head -n1 terminates as soon as head terminates?
Motivation: git log --grep="$MAIL_SUBJECT_LINE" --oneline --format=%H | head -n1 takes a long ...
0
votes
0
answers
82
views
.service file launching ROS2 nodes hangs early
I am trying to launch 2 nodes from a .service file on Ubuntu 22.04. The service file looks like:
[Unit]
Description=ROS2 Battery and Dock Monitor Nodes
After=network.target
[Service]
Type=oneshot
...
0
votes
2
answers
141
views
How can I test if a script was invoked using `. script.sh` vs `bash script.sh`?
I'm writing a script as part of a process to restore a broken environment. It sources a couple of other scripts, and I want all of those variables to be maintained on the command line after running ...
2
votes
3
answers
725
views
loop through if condition completely before else
I have large file with list of hosts. I remote to the severs to extract a variable which can end with odd or even number. I am running a for loop to execute same action(restart) on all variables ...
0
votes
1
answer
77
views
mv command from launchd
I'm trying to accomplish a simple task and getting stuck. I've written a very simple script
#!/bin/bash
cd /Users/nathan/Downloads/
mv test test2
mkdir test
This works when I manually invoke it. But ...
-1
votes
2
answers
96
views
Split string with 0-2 / (or determine there's none) (Bash)
Update: Up to 2 "/" in the string.
String structure is either:
Character set name/LF
Character set name/CRLF
Character set name/CRLF/(unknown purpose, likely a number)
Character set name
...
0
votes
0
answers
65
views
SSH Agent Scope Issue with i3 and Detached Process
I have an issue with scope of ssh-initialization.
I have following script for ssh-init.sh:
#!/bin/zsh
check-ssh-agent() {
[ -S "$SSH_AUTH_SOCK" ] && { ssh-add -l >& /dev/...
4
votes
3
answers
268
views
Add columns from variable number of files to base file
I'm dealing with a series of bed files, which look like this:
chr1 100 110 0.5
chr1 150 175 0.2
chr1 200 300 1.5
With the columns being chromosome, start, end, score. I have multiple different files ...
1
vote
2
answers
198
views
Get directory name and files within directories into a single output
I have a directory structure like this:
dir1/
732842342.mp4
screenshot1.png
dir2/
324324234.mp4
screenshot2.png
dir3/
121321321.mp4
screenshot3.png
What I'm trying to do is ...
5
votes
4
answers
994
views
Pass multiple files as a single option
I'm trying to build a wrapper to execute a tool multiple times, then concatenate some of the results. I'd like to pass two sets of files to my wrapper script, then run the tool for each pair of files. ...
-1
votes
1
answer
176
views
Best tool to convert xlsx to csv
I need to install something that allow me through a script bash or if I can do it without install any external tool it would be great.
The thing is I read about csvkit but is a tool of python and i ...
2
votes
2
answers
333
views
How to save the output of each iteration of a loop command to a different new folder?
I'm a Unix newbie so I apologize if the solution to this question is too easy.
I'm interested in running a script a number of times (because the process/algorithm doesn't produce exactly the same ...
0
votes
0
answers
19
views
Is there a canonical way to add a file to the "recently used" list from a shell script? [duplicate]
Well-behaved apps add the files they are saving to .local/share/recently-used.xbel so that they are easily retrieved when using another app (for instance, browser for upload).
So, if a shell script ...
3
votes
1
answer
980
views
Using make variable in bash scripting as part of a makefile command
I've created a makefile command to automate a publishing workflow using Pandoc and the Generic Preprocessor (GPP). It is as follows:
TODAY = $(shell date +'%Y%m%d-%H%M')
MACROS = utils/gpp/macros.md
...
1
vote
3
answers
196
views
Loop ip list through geoiplookup and delete lines that do not match criteria
Thanks in advance for any ideas you present.
My current project has me trying to loop a file containing a list of 1000's of IP addresses through geoiplookup and piping it to sed to delete all lines ...
0
votes
1
answer
64
views
What shell construct is being assigned to bash positional parameters and at what stage of command evaluation
Consider the 2 bash commands below:
$ VAR="hello world"
$ ./script.sh "$VAR"
Line 1 stores the string hello world (quotes removed) to parameter VAR.
Line 2 should be processed as ...
0
votes
1
answer
94
views
How to open line in $EDITOR while in python shell?
As a somewhat new user, I recently discovered the trick of opening the current terminal line in $EDITOR, using the hotkey C-x C-e. This has changed my life, and has made writing loops and such much ...
1
vote
3
answers
2k
views
Can .sh files only be used in Linux?
I just recently downloaded Linux, and overall it's not too bad, but I just have a question about .sh files and how they work:
when it comes to other files like .py, usually you'd be able to run them ...
0
votes
3
answers
290
views
Send input from a file into a single netcat connection via a Bash script
I have tried the following to no avail, the script just stops after the first input.
filename="..."
while IFS="" read -r line; do
echo "$line" | nc <target> <...
0
votes
0
answers
44
views
Error when indexing a Bash Array to echo or make a command [duplicate]
I have a shell script that I want to run many times. Each time, I want to use a different argument that comes from a CSV file. The file is very simple:
code1-code2-01
code1-code2-02
code1-code2-03
...
0
votes
2
answers
96
views
find script to sort subtree into most recent modification times?
What is the find script to sort a subtree into files with the most recent modification time? Also preserving their path name?
I suspect it has something to do with find, ls, awk and sort. I haven't ...
0
votes
1
answer
120
views
In Bash 5.2+, is there a means to read the command currently being evaluated?
...that is to say, is there a means by which I can read the full command that's being evaluated AS it's happening? Here's a contrived example, but one I feel makes the question clear:
function ...
0
votes
0
answers
63
views
Shell: redirecting output to /dev/tty and another file in a script? Not having to retype ending?
How do I accomplish this command to redirect command output to multiple files, without retyping the later A=1>&/dev/tty 1>&${TMP}/lastcmdout every time?
I want to see output on the ...
0
votes
1
answer
132
views
How I can run a php inline script and also provide command line arguments using bash?
I try to make a simple script that pings a network connection:
#!/usr/bin/env bash
# Xdebug ip detector for docker
# Copyright (C) 2023 Dimitrios Desyllas
#
# This program is free software: you can ...
0
votes
0
answers
59
views
Can't use combination of parentheses and variables in scripts [duplicate]
I have problems with replacing parentheses in combination with variables, in ssh.
For instance, replacing (hello123blablabla) with (hello). So, it replaces all together, and parentheses as well, at ...
1
vote
1
answer
147
views
Help figuring out how to handle errors in "mongodump" script
I have a simple bash script that's supposed to create a dump file from a database
docker exec -i container sh -c "mongodump --archive" > \
~/dumps/db-$(date + "%d%m%Y").dump ...
-5
votes
1
answer
107
views
how to check if a large script is only using full paths for the used cmd inside it?
to make a script more secure, we would need to confirm that used cmds, eg.: "/usr/bin/chown" instead of just "chown".
0
votes
1
answer
73
views
Commenting out a line item in a text file that my bash .sh file refers to
I have an ssh .sh bash script file i'm running on a cron to check website status. The bash script (when ran) refers to a local txt file for a list of websites to check if they're down or up. ...
-2
votes
2
answers
344
views
Dangerous behavior of the `cd` built-in
I was plodding along, working on a shell script, and I've just learned something that I found surprising. I'm presenting it here as a Question because I'd like to learn if there's some way to avoid ...
0
votes
2
answers
157
views
Can the main function of a bash script have the same name as the script?
Writing a quick bash function to change the network TTL (for tethering from a phone,) but as it turns out, it's a slightly different command for each system so to accomodate the logic, my simple ...
0
votes
0
answers
31
views
Does Nautilius the file manager customizer allow one to right click and pass into a python function the file right clicked as argument without error?
I am working on creating a way to right click on an .eml file, right click, and have python pull two pieces of data from it and paste them into a new xlsx file and auto open the xlsx file.
I would try ...
12
votes
2
answers
2k
views
How to automatically terminate shell scripts after 1 minute of no output
I need to run multiple shell scripts sequentially (s1.sh, s2.sh, s3.sh) with the following requirement:
If a script produces no output (stdout or stderr) for 1 minute, it should be terminated
After ...
0
votes
1
answer
195
views
How to properly make eval safe?
I'm writing a shell script that changes its behavior based on the number of positional parameters passed
script.sh
if [ $# -eq 1 ]; then
if [ -f "$1" ]; then
validate='validate <&...
4
votes
2
answers
846
views
Are Shell Script --long-options POSIX compatible?
In almost all POSIX specifications (e.g. ls), there are only short options (e.g. -A , -a , etc). In common shells like Bash, long options (e.g. --all , --almost-all , etc) are very common. And it is ...
0
votes
1
answer
817
views
How to use docker exec to execute a shell script with an argument in a container?
I have a shell script on my host, which is calling another script to run inside the container. Here is a simple case that works fine:
host_script.sh
#!/bin/sh
results=$(docker exec mycontainer "....
-2
votes
3
answers
235
views
Bash script to uncomment lines with leading spaces on a file with specific pattern
I try to uncomment specific lines from a file with patterns in oracle linux 8.6 using bash. There are leading white spaces on certain lines where the comments are not removed. I tried to uncomment the ...
-1
votes
1
answer
195
views
zip every month files and delete them [closed]
I have log patterns every day for artemis
I want to zip them and delete the files and save one month files in one zip
the files like this
artemis.log.2024-11-22
artemis.log.2024-11-23
artemis.log.2024-...
0
votes
4
answers
287
views
Clean up bash script
Can someone clean up this part of bin bash script for me? I feel like i can write this so much cleaner.
Basically, what i want to do is:
If $pluginexcl2 is defined that 2 different grep -v will be ...
12
votes
2
answers
2k
views
Merits of `cd && pwd` versus `dirname`
Some of my coworkers prefer to write the following at the top of their BASH scripts to determine the directory containing the script:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}&...
0
votes
1
answer
120
views
Looking for slideshow utility with custom/configurable randomisation
I'm looking for a way to provide custom/configurable randomisation to a slideshow tool like impressive or feh (something lightweight I can run automatically at startup). Basically I need to weight ...
0
votes
1
answer
75
views
How to start a screen session from entrypoint shell script, and make it persist?
I am starting a screen session in my entrypoint shell script in docker, then log the output of screen -list to a file.
#!/bin/bash
screen -S my_screen -dm bash -c 'cd project && npm run start'...
0
votes
2
answers
186
views
Displaying the sed command before its execution in a Bash script
I need advice on how to make every command I run in a Bash script first display itself and then execute. I created a simple function and a subsequent sed command to uncomment lines in the pacman.conf ...
1
vote
1
answer
203
views
Open pdf files by terminal with fzf and fd
with the following command line, which I turned into an alias, I would like to open the pdf files from my terminal.
alias pdfLoad="fd . '$HOME/OneDrive/DBCalibre' -t f -e pdf | fzf | xargs -0 -I ...
0
votes
1
answer
281
views
Find all files in directory and apply commands to each of them
I want to apply commands below to all files in a directory instead of one file.
cat file.txt | sed -E "s/\@([0-9]+)\W+~(.*?)/\1 \2/g" | tr -d '~'
cat file.txt | sed -E "s/\@([0-9]+).*\~...
1
vote
2
answers
86
views
Linux simple script: redirect error to file and email
I have a script that syncronizes folders. The script does
/usr/bin/rsync -av --delete $SOURCE $DEST >> $LOG_FILE 2>> $ERR_LOG_FILE
so it redirects output to LOG_FILE and error output to ...
0
votes
1
answer
52
views
Trying to sort with getopts with different commands and cannot sort by price
I am trying to sort an inventory list in unix/linux below. I am successful in sorting by name and ID and able to pull up the usage menu but cannot sort by price, even with an "awk" function....
0
votes
1
answer
70
views
Postfix Mail logs Alert
Is there a way via script to set an alert when an application is sending high volume of messages in short amount of time (5min)?
My goal here to have us notified when some application spamming or ...
0
votes
2
answers
136
views
Piping output to bash script
I'm going through the "sed & awk" book by Dougherty and Robbins. One of the examples calls for piping output to a shell script:
sed -f nameState list | byState
But I've found that, in ...
2
votes
1
answer
98
views
Get PID of command in one line bash script [duplicate]
When trying to get the PID of a shell command, it is possible using a script file:
#!/bin/bash
ls -lha &
echo "$!"
However, it is not possible using only the terminal.
/bin/bash -c 'ls ...
2
votes
3
answers
129
views
Printing a specific section everytime search results are matched
I have a pretty basic text file on a Linux machine that has stuff like Chapters, Dialogues and References.
This is what it looks like
Chapter: 1 One: Birds and Trees
Birds are beautiful and trees ...