Questions tagged [bash-scripting]
Bash scripting is making scripts in the Bash shell language.
973 questions
6 votes
3 answers
568 views
How to use BASH variables with "local", but less bloated?
I have a lot of code with lines like the following: local -r foo="$(bar)" Just recently I discovered shellcheck and that warns about those assignments, because local hides the actual error ...
0 votes
0 answers
72 views
How to use NinjaRMM to run an application via API?
I have a script like this listed on the device: { "type": "SCRIPT", "id": 67, "name": "script.run_application", "language&...
2 votes
1 answer
135 views
How do I write a script that logs me out and suspends a PC running Ubuntu 24 with LightDM?
I'd like to automate logging out and suspending the PC on Ubuntu 24 with LightDM. I tried this: SESSION_ID=$(loginctl | grep $(whoami) | awk '{print $1}') loginctl terminate-session "$SESSION_ID&...
9 votes
10 answers
2k views
How to compare numeric strings in Bash?
I am attempting to write a script depending on the version of Debian. I have tried the following: #! /bin/bash DEBVERS=$(awk '{print $1}' /etc/debian_version) echo "DEBVERS = " $DEBVERS if [[...
2 votes
1 answer
177 views
Bash script iterate through directory and delete zip file
Good day, I have a directory with all my customers. (Auto Electrical). Inside every customer's directory is another directory with every booking they have booked with me before: -John_Smith --...
2 votes
2 answers
185 views
Multiple result lines from bash feeding into multiple variables
I am new to Bash and wanted some help. I have a command which produces four lines and I would like these lines to be fed to four variables, so i can use them in the next command. could someone please ...
0 votes
0 answers
60 views
Are xarg + cp vulnerabler to race conditions?
I'm experimenting with Docker images having 20-70 GiB in a single layer (yes, the software/compiler is so fat). To improve the download and decompression efficiency of Docker, I'm splitting this fat ...
0 votes
2 answers
120 views
preserve variable within bash script
I have a bash script I am using to deploy updates automatically. It also includes logs of when the updates have been run. I have added the entire bash script below. The issue I am having is within ...
0 votes
0 answers
93 views
Jenkins Groovy syntax not working for find command
I'm trying to use find command with a backslash on the command below at groovy syntax: stage("ArtifactRename") { sh '''find "./os/mcu/GNU ARM v10.2.1 - Debug" -name "\\...
1 vote
1 answer
87 views
The output of a bash command changes if executed by a script
~$ echo $(date +%a%d\ %X) lun.24 17:40:28 ~$ inode-1 thunar.desktop lun.24% +17:40:40 ~$ cat inode-1 #/bin/bash # inode_check xdg-mime query default inode/directory >> /home/<user>/f1; ...
-1 votes
1 answer
401 views
Will jq output valid json even if error?
Is jq guaranteed to always output valid json, even if it encounters an error while running? Error could be due to poorly formatted input, for example. Outputting valid json and not outputting anything ...
1 vote
0 answers
433 views
Store bash glob pattern in a variable
I can use syntax as below to list 6 files named a1b.md, a2b.md, a3b.md, x1y.md, x2y.md, x3y.md in a directory: ls a{1,2,3}b.md x{1,2,3}y.md I'd like to use something like this in a bash script; with ...
0 votes
1 answer
199 views
How to run ncat -zv without showing version on RHEL7?
Hi everyone and thank you in advance! I used to run the following script with ncat Version 5.51 and it gave me wonderful formatting [root@server user]# for i in "cat server_list" ; do echo $...
1 vote
1 answer
180 views
Exit code of a bash script when one of the steps fails?
So I have to run a basic shell script that is not much more than: #!/bin/bash call01 call02 ... call_N If one of the calls fails (gets killed by the OS because it runs out of memory), what does it ...
3 votes
1 answer
119 views
Do until either n seconds have passed or any key has been pressed
How can I interrupt a bash script until either n seconds have passed or any key has been pressed? What I have been trying so far was to combine a loop like while [ $n -gt 0 ] ; do n=$(($n-1)) ...
1 vote
0 answers
112 views
How can I cap video length to just slightly below 60 seconds without re-encoding with FFmpeg?
I'm currently making a script to cap my videos at just slightly below 60 seconds. So if I have a video clip that is exactly 60 seconds long, I trim it by exactly 1 frame so it is slightly below 60 ...
0 votes
1 answer
323 views
Returning a Value from an external operation in shell script in Linux?
I am writing a shell script in Bash. Within the Bash terminal I am using "CLI" commands to do some function. In this example I am creating a model, that if the syntax is correct, will ...
1 vote
0 answers
58 views
Can I invoke xtrace to attach to an already running process
In bash scripts I sometimes use a command of the form xtrace -o xtr.txt 2> /dev/null command-of-interest to obtain info about 'command-of-interest' that is otherwise unobtainable. But this starts ...
0 votes
0 answers
328 views
How to perform a zuluCrypt "Create Encrypted Container in Hard Drive" but as a Linux command line?
I have long been in the process of converting my manual steps to create a new backup disks into an automated script. The only part I have left is the initial "creation" of the disk, meaning ...
2 votes
0 answers
415 views
Why is GNU wget only returning an index.html.tmp file, instead of the actual files I want?
I want to download a lot of satellite data (organized by each day of each year) in .hdf format from NASA's LAADS DAAC archive. They have a helpful guide that provides a code sample to use with GNU ...
0 votes
1 answer
839 views
Restart autosys job after a fixed delay
I have a scenario where I have to retry run the failed autosys command job after 10 minutes. I have to retry rerunning it only twice. I cannot use n_retry attribute here because I need to wait for 10 ...
0 votes
1 answer
138 views
SOX do not want name files
Want to record a radio broadcast into separated files. receiver | sox -traw -r8k -es -b16 -c1 - -b16 -es -c1 -r48000 -t wav ~/myfolder/$(date +%H_%M_%S).wav silence 1 0.10 0.1% 1 1 0.1% : newfile : ...
0 votes
0 answers
147 views
bash sourced command silently fails under WSL2 vs direct execution from shell?
I have a Bash script which evaluates JSON file to set environment variables and creates a function The script works fine under native Ubuntu (22.04LTS), but when you run the same script under WSL2 (...
0 votes
1 answer
241 views
Match a User That Starts With a Single Letter
I've been handed a script written by another and been asked to modify it according to their needs. There's one line that needs to be altered: if [ "$user" == "nobody" ] This ...
1 vote
0 answers
1k views
Separating audio m4b file based on a cue file
Separating audio m4b file based on a cue file How can I split an audio m4b into separate files using a cue file? Note: The m4b file has no chapters that's why I need to use the cue file to split it. I ...
0 votes
3 answers
133 views
Why doesn't `bash` work with piped output, when the output is as expected?
I'm running the following command to alias an entire folder of scripts: ls | grep '\.sh$' | sed 's/\.sh$//' | awk '{print "alias "$1"=\"./"$0".sh\""}' | xargs -...
2 votes
1 answer
666 views
Execute self-extracting bash script (with binary data appended) w/o saving it first
I have a self-extracting bash script (binary data appended to the end) like this: #!/bin/bash export TMPDIR=$(mktemp -d) ARCHIVE_START_LINE=$(...) # <omitted> # extracting data archive into a ...
1 vote
1 answer
66 views
Moving directories that names end in - number matching a list.txt file
Graphic of Folder Structure : I am very new to advanced shell commands and scripting. I have fundamental terminal knowledge but have never attempted to create an automation like this before. A pure ...
1 vote
0 answers
689 views
MPV to play videos with a geometry
I have YOCTO build and I have integrated the mpv to the image. I need to test something so, I wrote a script that could play videos with a certain width and height and position on the screen but mpv ...
0 votes
2 answers
823 views
Bash provide comma-separated list in multiple lines
I have a bash script that is suppose to run another program called file-selector. #!/bin/bash ... ./file-selector -exclude="a/b/c/d/e/f1,a/b/c/d/e/f2,a/b/c/d/e/f3,a/b/c/d/e/f4,a/b/c/d/e/f5,a/b/c/...
1 vote
1 answer
105 views
merge numbered files from different directories into same directory in correct order
I have some directories named FOO and BAR and etc. that have files in form FOO/ 1.png ... 18.png BAR/ 1.jpg ... 12.jpg ... and I would like them to be all copied into the DESTINATION directory ...
0 votes
1 answer
366 views
The last command output in linux for reboot is not giving the uptime instead it says still running twice
[root@localhost test]# last -x reboot -s -30days reboot system boot 5.14.0-325.el9.x Sun Mar 10 10:51 still running reboot system boot 5.14.0-325.el9.x Tue Mar 5 03:05 still running In the ...
1 vote
1 answer
70 views
shell helper function wrapping `find` and ignoring some dirs
I have a bunch of scripts which follow the pattern of: find files which match a pattern do something The problem is that there are several "hazard" directories which are large and slow, ...
0 votes
0 answers
207 views
How to run a batch script completely and automatically on event such as sleep/hibernate and shutdown event using task scheduler?
I have written a batch script to perform some folder synchronizing task. I have scheduled the script to execute using task scheduler on events such as sleep/hibernate(kernel power event id 42) and ...
0 votes
1 answer
63 views
Why directories are not being expanded and some others are not created?
I am using Docker and I am trying to run the following script in the container which uses Alpine Linux and sh as a shell: Note: SCANDISK_MOUNT and SCAN_MOUNT are environment variables defined as var/...
3 votes
3 answers
1k views
Bash [[ test =~ regex ]] vs perl command result
Is the bash operator =~ equivalent to a perl invocation? filename="test-33.csv" regex="([^.]+)(-\d{1,5})(\.csv)" With bash test: if [[ "$filename" =~ $regex ]]; then ...
1 vote
1 answer
489 views
How to get the timestamp for the system lock/unlock of ubuntu 22.04 with the user's inactivity time? (Not login/logout.)
I want to get the user's lock & unlock timestamp in a single script or command, as in "last" command I can see the user's name with system's login & logout timestamp. How can I get ...
0 votes
1 answer
201 views
Loop of screens with a loop inside does not pick variable values
How can we execute a loop of screens, each of them executing a loop so that it picks both inner and outer variables? Consider this: # does not print i for i in 1 2; do screen -dmS screen-${i} ...
0 votes
1 answer
180 views
How to convert code from test format to if fi format?
I have code that opens a bash file in a terminal for entering the sudo password: test "$IN_TERM" || { export IN_TERM=1 konsole -e "$0" exit 0 } && true I need ...
-1 votes
1 answer
59 views
How to get this desired output
I have this file: Alias name: ciscobas Owner: CN=DI0DGQB2QG9RK8OHMJ5C, O=Duo Security Issuer: CN=DI0DGQB2QG9RK8OHMJ5C, O=Duo Security Valid from: Thu Jun 15 15:08:49 AST 2023 until: Tue Jan 19 06:14:...
0 votes
1 answer
806 views
Reboot in middle of bash script
I'm writing a bash script to setup some services in Ubuntu. I require some system reboots between some steps. The problem with this is running reboot interrupts the script as well. Is there a way for ...
2 votes
0 answers
518 views
How can I close a browser window from a bash script?
I would like to open and close windows from a bash script but in my two versions of the code it gets stuck when it opens the first window and does not close the current window and then open the new ...
0 votes
1 answer
490 views
Getting udev to check audio devices
I'm trying to write a udev rule for my bluetooth receiver which, when it connects, runs a script that checks whether a corresponding audio device has also been added, and reconnects if that is not the ...
0 votes
1 answer
184 views
How deep is too deep for Nested Folders in a Bash program?
I am somewhat new to Bash and am writing a program to install .NET SDK and Runtime on each of the available Linux distros. The problem I am having is that I am trying to source functions from two ...
0 votes
0 answers
79 views
How to combine 2 rsync outputs into 1 command?
using: neofetch --stdout |grep 'OS:' OS: Kubuntu 22.04.3 LTS x86_64 Shell: bash 5.1.16 rsync --version rsync version 3.2.7 protocol version 31 variables, SourceDisK1 src1 and destination ...
1 vote
2 answers
5k views
How can I use Windows command line to delete all files in a particular directory that were created on a specific date?
I accidentally blew up a tarball on my PC and now have something like 2 million unwanted files in my Documents folder. Windows Explorer crashes if I try to use it to view the contents of that folder, ...
-1 votes
1 answer
4k views
Echo all the output from a ls command
I have the follow files in my folder: a.jpg b.jpg c.jpg And I have the follow script: #!/bin/bash echo $1 If I run: script.sh $(ls) My output is: a.jpg But I want to be: a.jpg b.jpg c.jpg So, Why ...
0 votes
1 answer
109 views
Complex find statement
Have a script that I use to purge old backup directories. All the directories are named YYYY-MM-DD. The backup directories have links in them as the backups are incremental using the rsync --link-dest....
0 votes
1 answer
123 views
rm not removing directory
Having a strange issue. Using a script to go through our backup folders and delete any folder not named like the 1st of the month (2023-05-01) and older than 60 days. It works in the sense that it is ...
2 votes
0 answers
182 views
How do you set qpdfview display options in a bash script?
Pseudocode: qpdfview **--continuous --2page-w-cover --zoom=167%** document-one.pdf#54 (or document-one.pdf#src:foobar.tex:42:0) & disown printf ' \n \n Some nice message \n \n ' read -p "...