Tracing the bash startup process
One question I occasionally see in other forums:
>HELP! I'm getting this `<command>`-related error on login, but I can't figure out *where* `<command>` is being invoked!!!
There's a one-liner for that, inspired by [this StackExchange answer](https://unix.stackexchange.com/a/154971/98480):^(1)
PS4='+${BASH_SOURCE}:${LINENO}> ' BASH_XTRACEFD=9 bash -xli </dev/null 9>/tmp/xtrace.log
then look in `/tmp/xtrace.log` to see the extended trace of their shell startup, complete with startup script filepaths, line numbers and call-depth indentation:
$ head /tmp/xtrace.log
+/etc/profile:4> '[' '\s-\v\$ ' ']'
+/etc/profile:5> '[' /home/linuxbrew/.linuxbrew/bin/bash ']'
+/etc/profile:5> '[' /home/linuxbrew/.linuxbrew/bin/bash '!=' /bin/sh ']'
+/etc/profile:8> '[' -f /etc/bash.bashrc ']'
+/etc/profile:9> . /etc/bash.bashrc
++/etc/bash.bashrc:7> '[' -z '\s-\v\$ ' ']'
++/etc/bash.bashrc:11> shopt -s checkwinsize
++/etc/bash.bashrc:14> '[' -z '' ']'
++/etc/bash.bashrc:14> '[' -r /etc/debian_chroot ']'
++/etc/bash.bashrc:20> '[' -n '' -a -n '' ']'
If you've refactored your shell startup into a branching tree of scripts (like me), this should help bring some sanity back into your debugging.
In a nutshell, this one-liner invokes a `bash` in xtrace'd interactive login mode, that automatically exits at the end of the startup process^(2) and logs xtrace output with informative decorations to `/tmp/xtrace.log` on FD 9.
Note that this leaves your normal startup output on your console, so it's easier to look through the trace without all that output clutter. Sometimes, though, you don't even know which program is causing the error. In that case, you want normal and xtrace output interleaved, like what you'd see with `bash -x`:
PS4='+${BASH_SOURCE}:${LINENO}> ' bash -xli </dev/null >&/tmp/xtrace.log
Comments welcome.
# Footnotes
^(1) No, `${BASH_SOURCE}` and `${LINENO}` aren't typos. `LINENO` is not an array (you're probably thinking of `BASH_LINENO`), and while `BASH_SOURCE` is one, this reference is exactly equivalent to `${BASH_SOURCE[0]}`, and I'm lazy that way.
^(2) If your startup involves user input (e.g. secure-mount passwords), you'll have to remove the `/dev/null` redirection, and remember to type `exit` (or hit `Ctrl-D`) when the startup process is done.
https://redd.it/1b8rev9
@r_bash
$(echo $variable | some command) - help
```
read -rp 'Enter your repository clone address - eg: https://<PAT>@github.com/username/repo.git : ' git_address
crypt_git_address=$(echo -n $git_address | openssl enc -aes-256-ctr -pbkdf2 -a -k apples)
sed -i "13 s#^#crypt_git_address="$crypt_git_address"\n#" ./mkdocs.sh
printf 'done\n'
```
echo -n $git_address is not appreciated - how do I make this work?
Thanks
https://redd.it/1b8dowf
@r_bash
"Lock a resource" using bash
Hello everyone!
I have an application that is used by multiple users but cannot be used at the same time and I'm trying to figure out a way to lock this resource using bash.
My idea is to have something like:
lock_resourse.sh -t timeout
command_1
command_2
command_3
release_lock.sh
I'm currently achieving this by using a temporary file, but I know it's not ideal as these operations are not atomic (at least I think they are not). I've taken a look at flock, but I don't think it can be used the way I'm currently planning on using it.
My current implementation:
lock\_resource.sh
SECONDS=0
while [ -f $LOCK_FILE ]
do
if [ $SECONDS -gt $LOCK_TIMEOUT ];
then
echo "Lock not acquired."
exit 1
fi
sleep 1
done
# Create lock file.
touch $LOCK_FILE
echo "Lock acquired."
release\_lock.sh
rm -f ${LOCK_FILE}
Does anyone have any alternatives to this implementation?
Thank you in advance.
https://redd.it/1b82vfc
@r_bash
How can I convert bash script with FFmpeg into a Windows one?
I'm trying to convert an FFmpeg command created with a bash script to Windows. I'm having trouble running the command as a Windows .bat file. I think I need to rewrite it into a script which I'll run on Windows. I don't know how to do it.
Is there an alternative way to do this?
I'd appreciate any help.
https://redd.it/1b7wo2d
@r_bash
How would I go about showing different things on the same line over time?
Let's say I wanna make something that requires a times between two actions.
The easiest would probably be figuring out a way to show an actual timer, starting from 10 and going to zero or something.
But let's assume I want to do this in a less boring way and, instead, I want to show a bunch of different faces in ascii.
How do I make it so they appear on the same line, replacing the face that came before and without clearning the whole console output?
I'm absolutely new to bash scripting so I apologize if this is something that would be way out of a noob's reach.
https://redd.it/1b71520
@r_bash
Search for User Function
Hello!
I'm trying to build a script that Installs multiple programs in one go, I have a v1 that does that but zi was thrown a curveball and need some advice. I will have two different types of terminals. One will install as root, and the other will require sudo. Is there a way for a function to run the whoami command, see that it's root and check the /home/ directory and verify the user account that way?
https://redd.it/1b6swu0
@r_bash
Shell Scripting, SED and AWK
https://coursegalaxy.newzenler.com/courses/shell-scripting-sed-awk?coupon=SHELLSCRIPT30
https://redd.it/1b6gx9e
@r_bash
In bash, I noticed at least 3 ways to structure a function, which way is best?
Each of the following way of structuring functions within a bash script are equally valid. What I am wondering is if one is better than the other.<funct_name> () { <code> }
function <funct_name> { <code> }
function <funct_name> () { <code> }
Is one situation better for a certain circumstance within script than another? Does the call or location of the call matter within script or how the return is handled? Or, is there any difference at all? If not, why are all three legal?
https://redd.it/1b4w0ee
@r_bash
spin - execute a command in the background and display a scrolling tail of logs
https://github.com/amancevice/spin
https://redd.it/1b4prg0
@r_bash
Non-US: which keyboard layout do you use?
Do yo use your Country language keyboard or English International? Mostly related to system / network admin job like shell than other programming languages.
https://redd.it/1b4m0c1
@r_bash
Shell hangs when reading output of a program that suspends
I have an issue when trying to assign a program's output to a variable. If the said program suspends the whole terminal hangs indefinitely. The program is a TUI application. I have encountered such behavior with several different TUI programs and have a couple of questions in regards to this.
It goes something like this:
#! /bin/bash
output=$(program)
# program suspends
# terminal becomes unresponsive
echo $output
1) Is this expected (normal) behavior? (Perhaps I am violating some rule and using the shell inappropriately)
2) What actually is happening under the hood?
3) Is there a way to resolve this or a work around?
What I need to do, is to store the output of a TUI program, that may or may not suspend several times, and then use that output in another program / command. Perhaps there is a better way to do this, but due to my inexperience, I haven't been able to come up with anything else. I am grateful for any kind of help / suggestions / feedback.
https://redd.it/1b3z2tr
@r_bash
found
bash: line 7: gcc.exe:: command not found
bash: line 8: compilation: command not found
Compilation successful
Project tarballed and gzipped successfully.
user@server.university.edu's password:
MyRealNameCSCI2240PROJECTNAME.tar.gz 100% 648 32.3KB/s 00:00
Project successfully transferred back to the local machine, go ahead and turn it in.
# File Structure
1. `bark.c` is a compilable, simple, lightweight C program that has been compiled properly on the remote server before
2. The file structure of my server can be described as there is one folder csci2240 with nothing in it (except for this mess). That's not causing any problems
3. The executable does not appear in my remote server:
​
user@server:~/csci2240/PROJECTNAME$ ls
PROJECTNAME.c
user@server:~/csci2240/PROJECTNAME$
# Other Notes
1. I can compile directly with the same command on the remote server without any issues after the program the shell script) executes
2. Obviously there's extensions for this stuff (Like Microsoft remote server) but we're not allowed to use those kinda things, and while there is obviously other extensions that I've now found out about and downloaded-- I just want the script I wrote to work now.
3. I believe the error in the program is occurring on the compile (line 29), since the previous echo command occurs without errors, and the compiling error echo does not appear, but the compilation successful error does (even though it obviously wasn't successful).
4. I'm using GIT Bash, and the remote server for my university uses a linux command line system (bash).
This script was partially made by ChatGPT, not just me. All in all about 30% me 70% GPT. I didn't even know the commands at first so I had GPT explain them to me and then the guy in the youtube video that I sat and watched for a few hours.
https://redd.it/1b3cn2l
@r_bash
Newbie with Bash - Help with Syntax Error
Hello all,
I'm working my way through learnyoubash exercises as a complete beginner, and am having some trouble with the arithmetic expansion for one of the the exercises.
I've tried:
#! /usr/bin/env bash
result=$(( ($3 + $2) $1 ))
echo $result
and get the following error every time:
"line 3: ( + ) \ : syntax error: operand expected (error token is ") * ")"
I've been tinkering around with the syntax for a while now but somehow can't get it to fix the syntax.
Any assistance is greatly appreciated!
​
https://redd.it/1b3d2ms
@r_bash
Why is my command in Interruptible Sleep and how do I wake it?
I have a set of iqtree commands that I've been checking in on a remote computer, and sometimes they will go in and out of interruptible sleep for a reason I don't know.
The top line is coming from the log of a sleeping process, and I can see that the time does not change, so it's not running and updating the log. The reason why I'm asking is because a previous run took a week, but was only running for \~114 hours, a couple days short of a week. It doesn't seem like much, but I'd like to have that information back as soon as possible so that I can see what needs to be fixed before trying another iteration.
Is this normal? Is there a way to manually wake up these commands if I see them sleeping? For refence the remote computer has 48 threads and I've already cleared up with someone else that this isn't a CPU usage problem.
https://preview.redd.it/4o7d3bipsklc1.png?width=1608&format=png&auto=webp&s=89c8af972dabeb1868e93e1dbdfa732a5b6eb3f7
https://redd.it/1b39775
@r_bash
while true error
I'm trying to make a pomodoro timer that runs on my terminal.
The code ive written till now is this:
#!/bin/bash
timer() {
start="$(( $(date '+%s') + $1))"
while $start -ge $(date +%s) ; do
time="$(( $start - $(date +%s) ))"
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
sleep 0.1
done
}
stopwatch() {
start=$(date +%s)
while true; do
time="$(( $(date +%s) - $start))"
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
sleep 0.1
done
}
while :
do
if [ -n "$1" ]
then
timer $1
if [ -n "$2" ]
then
timer $2
else
stopwatch
fi
done
when i run this program i get a error saying syntax error near unexpected token 'done'
. I dont understand this as i think done is needed there to signify end of loop?
https://redd.it/1b2yb60
@r_bash
My start template for writing a bash script
#!/bin/bash
initialize() {
echo "Directions"
DIR="$( cd "$( dirname "${BASHSOURCE[0]}" )" && pwd )"
cd ${DIR}
}
vars() {
readonly ARGS=("$@") #make parameters global and put it in an array
# command line arg values can be accesed by ${ARGS[0]} ${ARGS[0]}
}
#sample functions
getipaddress() {
local domain="$1"
local ipaddress=$(dig +short $domainname | grep -Eo '(0-9{1,3}\.){3}0-9{1,3}' | head -n 1)
echo "$ipaddress"
}
#main procedure
main() {
initialize
vars $@
ipAddress=$(getipaddress ${ARGS0})
}
main $@
In this way, I write all functionalities as functions and then call them in the main() block.
This forces me to write with a structure. No more spaghetti!
https://redd.it/1b8l5bb
@r_bash
Need shell script to send outlook mail via oauth2
Anyone have shell script to send outlook email via oauth2 ?
https://redd.it/1b8142o
@r_bash
Error with Bash and OpenSSL.
I am writing a personal script to generate SSL certificates with OpenSSL from a config file but I keep getting the below error when generating the certificates.
Code being run:
openssl req -new -x509 -sha256 -days 365 -subj "$CASubj" -passin pass:$PW -key ./Keys/${CACN// /}-ca-key.pem -out ./Certs/${CACN// /}-ca.crt
Error out:
+ openssl req -new -x509 -sha256 -days 365 -subj '/CN=GTS Root R1/C=US/O=Google Trust Services LLC' -passin 'pass:JIU(fhe)boibbviyv8b' -key ./Keys/GTSRootR1-ca-key.pem -out ./Certs/GTSRootR1-ca.crt
name is expected to be in the format /type0=value0/type1=value1/type2=... where characters may be escaped by \. This name is not in that format: 'F:/Program Files/Git/CN=GTS Root R1/C=US/O=Google Trust Services LLC'
problems making Certificate Request
I am using Git Bash on a Windows 10 PC and wanting to have the certs and keys in seperate folders for cleanliness. Can anyone figure out why the the certificate subject keeps pulling the Git Bash directory into the variable being used (only when in quote "") even though the debug output shows the correct subject? I have even tried using the below code but get the same result.
openssl req -new -x509 -sha256 -days 365 -subj "${CASubj}" -passin pass:$PW -key ./Keys/${CACN// /}-ca-key.pem -out ./Certs/${CACN// /}-ca.crt
https://redd.it/1b824cd
@r_bash
I have written a nice little bash script that is for running rsync on media drives from a media host to either all media drives, one media drive, multiple media drives or directory to media drive.
I am rather proud of this script.
I was looking for a way to choose a host USB peripheral, find other connected peripherals and list ones that are large enough potentially to hold a one-to-one backup.
Used size on the host would be compared to the total size of the potential drives and would eliminate any drive smaller than the used space on the host and could not take a rsync backup.
The script also looks to see whether there is a dual boot with Windows OS drive connected in a dual boot environment. It finds the C:\ drive location using df to first search for a Microsoft reserved
type partition and uses that to determine the the physical drive location of the Windows OS, then searches for Microsoft basic data
using the partition info from the above action, looks to see if it's mounted, then unmounts it if it is mounted. It also write the filesystem path of the Windows OS to a variable that eliminates the filesystem location from the search of potential destinations, to make sure it is not messed with. Windows can be touchy if the wrong file is messed with.
The script lets you choose one or more drives from a list of those capable of taking a complete backup; a back up to all connected drives; or a single directory to a drive.
For instance, if it finds 5 connected USB or backup drive peripherals, (it tells you the total size of the drive, used space and available space) and if you wanted to run rsync on drives 1, 3, and 5 you can do that just by entering 1 3 5
, and it will cycle through, running rsync on each.
I currently have a menu listing for backup of the host to:
1) All drives,
2) Single directory to a all drives, and 3) Single directory to a single drive,
and I will be adding a selection for a single directory to a single directory of the user's choosing shortly as my next assignment.
I am looking to let others to try and see if it can be useful for them, and maybe suggest some enhancements . It's standard bash script in .sh file. Would GitHub be a good place to share it? Or is there somewhere else that would be good as well that is well suited for a project like this? I don't have a name for the script other than alldrivesync.sh, any suggestions?
https://redd.it/1b7llse
@r_bash
Need help with imagemagick
Hello all,
I am noob in bash scripts, and I need your help guys. I am trying to configure Azure Linux web server, and I am almost all done somehow, but what bugs me is imagemagick installation. In Azure there is a custom.sh file in which I include commands when server startup ,this is command list:
apt-get update
apt-get install rsync -y
apt-get install cron -yqq
crontab -l | { cat; echo "*/5 * * * * /usr/local/bin/php /home/site/wwwroot/path/to/file scheduler:run"; } | crontab -
service cron start
apt-get install imagemagick
apt-get install mysql-server
apt-get install sshpass
/usr/sbin/apache2ctl -D FOREGROUND
And everything works just fine except imagemagick, when I try to install it through ssh command line it works, but it ask me to download additional files and I need to confirm that with "Y", so most probably that is a reason why its not installed on startup.
Is there any way to install this without confirmation, i need to pass something else in command?
Thank you very much in advance
https://redd.it/1b71le2
@r_bash
Search for User Function
Hello!
I'm trying to build a script that Installs multiple programs in one go, I have a v1 that does that but zi was thrown a curveball and need some advice. I will have two different types of terminals. One will install as root, and the other will require sudo. Is there a way for a function to run the whoami command, see that it's root and check the /home/ directory and verify the user account that way?
https://redd.it/1b6swy4
@r_bash
How do I in a bash script, generate a list of a-z, small letters, two characters?
Example:
aa
ab
ac
...
zz
https://redd.it/1b4xz2f
@r_bash
I made a frontal version of the bash icon for better visibility in small icons
https://redd.it/1b4rtn1
@r_bash
dd if=X vs dd <X
Is there any reason to prefer one of dd if=X
and dd <X
vs the other?
https://redd.it/1b4o0jz
@r_bash
how to execute two commands at the EXACT same time in bash?
I know about semicolons and & but they dont execute commands at the exact same time in bash.
Is there a way to execute two commands at the same time in bash? or in any other posix shell?
##### example:
CMD1: printf "\e[10;10HHello World\n"
CMD2: printf "\e[20;20HGoodbye World\n"
https://redd.it/1b48p40
@r_bash
How to set up aliases for commands with options
Say I want my `ls` command to alias to `exa`. I set it up inside the bashrc file. but when I do `ls -l` it shows me the standard output instead of `exa -l`. What changes do I have to make to the alias to remedy this.
I feel this is a very simple problem but I'm not technical enough to figure it out myself and everywhere I've looked all the ways are to setup normal aliases, so tia if someone can help me out.
https://redd.it/1b3mzk3
@r_bash
Looking for help on a program:
So I've done 3 things to try and fix this script I wrote up. I took a bash tutorial (it was hours long), I asked chatgpt, I went to my university's tutoring whatchamacallit, I ate a cookie, stared at the script for nearly 2 hours. I don't get what I'm doing wrong (obviously part of it was the cookie...).
The whole thing looks right. But what I'm getting as an error isn't a lack of permissions or anything, it's just that it doesn't workbecause a command is missing. I put my code down below and the error I'm getting (obviously some censored information is there, but we all know that the censored information isn't the problem).
# Program Brief
My Intro to C programming class has us turn in our assignments through 6 steps:
1. Make the program on our computers (compliant to C90 standards) in whatever IDE we like.
2. Send the program to our university's remote server
3. Compile the program (with specific flags and restrictions) on our university's remote server to generate an executable.
4. With the executable and the file in a folder (on their own together) tarball it and gzip it.
5. Bring the gzipped tarball of the file back onto our machines
6. Turn them in on canvas (not part of the script, though it would be nice-- but too complicated).
# The Shell Script
#!/bin/bash
# GET INFO
read -p "Enter the name of the file on your local machine: " localfile
read -p "Enter the name of your project: " projectname
# PATHS/INFO
localpath=$(pwd)
remotepath="/home/myuser/csci2240"
remoteuser="myuser"
remotehost="server.university.edu"
remotefile="${projectname}.c"
echo "Starting the file transfer process..."
scp "${localpath}/${localfile}" "${remoteuser}@${remotehost}:${remotepath}/${remotefile}"
scpstatus=$?
if [ $scpstatus -ne 0 ]; then
echo "File transfer failed. Something went wrong."
exit 1
fi
echo "File transferred to ${remoteuser}@${remotehost}:${remotepath}/${remotefile}"
ssh "${remoteuser}@${remotehost}" bash <<EOF
cd csci2240 || exit
mkdir "${projectname}" && mv "${projectname}.c" "${projectname}"
cd "${projectname}" || exit
echo "organized and set up to compile"
echo "compiling project now"
gccoutput=$(gcc -Wall -Wstrict-prototypes -ansi -pedantic-errors -o "${projectname}" "${projectname}.c" 2>&1)
if [[ ! -z "\$gccoutput" ]]; then
echo "\$gccoutput"
rm -rf "../${projectname}"
echo compiling error occurred
exit 1
fi
echo Compilation successful
cd ..
tar czf "MyActualNameCSCI2240${projectname}.tar.gz" "${projectname}" 2>&1
gzipstatus=\$?
if [ \$gzipstatus -ne 0 ]; then
echo "An error occurred in tarballing or gzipping."
exit 1
fi
echo "Project tarballed and gzipped successfully."
EOF
sshstatus=$?
# Check if SSH command was successful
if [ $sshstatus -ne 0 ]; then
echo "An error occurred during remote operations"
fi
scp "${remoteuser}@${remotehost}:${remotepath}/MyActualNameCSCI2240${projectname}.tar.gz" "${localpath}"
scpstatus=$?
if $scp_status -ne 0; then
echo "Issue occurred with transfering file back to local machine. "
fi
echo "Project successfully transferred back to the local machine, go ahead and turn it in."
# The Error
user@computer MINGW64 ~/Music/managingdatabsesomething
$ ./turnintry2.sh
Enter the name of the file on your local machine: bark.c
Enter the name of your project: PROJECTNAME
Starting the file transfer process...
username@server.university.edu's password:
bark.c 100% 867 146.4KB/s 00:00
File transferred to user@server.university.edu:/home/user/csci2240/PROJECTNAME.c
user@server.university.edu's password:
organized and set up to compile
compiling project now
bash: line 6: error:: command not
Okay so... I think I'm going mad with the simplest task
I wrote several scripts in the past but this is driving me nuts. I'm trying to do the simplest thing possible, cding to the most recent folder created. I don't even check if its a file. I have cd aliased to zoxide in zsh but, in theory, it shouldn't matter. I've tried running the script in bash, sh and dash with similar results (changing the header basically, afaik this is POSIX, at least its simplest form). I've also tried some similar scripts with no result. When I execute the commands on the command line it does work. Basically it's this:
#!/bin/bash
MY_DIRECTORY=$(ls -t | head -1);
cd "$MY_DIRECTORY";
echo "cd to $MY_DIRECTORY";
I'm by no means a bash expert so I though it could be the usage of ' " ', I've gone through all (?) of the possibilities and no results.
I create a directory called asdf and it says "cd to asdf" but nothing happens. What's going on?
https://redd.it/1b37fbg
@r_bash
White House urges to stop using C and C++
https://www.infoworld.com/article/3713203/white-house-urges-developers-to-dump-c-and-c.html
https://redd.it/1b2on2s
@r_bash