r_bash – Telegram
Variable with single quotes causes odd behavior

Background:


I’m writing a noscript that prompts the user to enter a username and a password to connect to an smb share. The supplied credentials are then passed to a tool called smbmap.

I wanted to wrap their input in single quotes in case there are any special characters. When I’m using the tool manually, I put the username and password inside single quotes & it always works.

When I run smbmap using my noscript it fails if I add the single quotes, but works if I don’t add them.

I’ve tried having the user manually enter the credentials with quotes (e.g. ‘Password123’), & I’ve also tried things like:

read passwd

login=“‘“

login+=$passwd

login+=“‘“



smbmap -H IP -u $user -p $login

I’ve done this exact thing for other tools & it always works.

TL;DR

I can manually use a tool with single quotes around argument values, or I can use variables for argument values, but can’t do both.

Why does adding the single quotes change the behavior of my noscript? I’ve literally done echo $login, copy/pasted the value into smbmap & successfully run it manually.

I’d really appreciate any insight! I’m totally perplexed

https://redd.it/1fcy7ge
@r_bash
I'm new to bash and noscripting and need help

i'm trying to do an ip sweep with bash and i ran into some problems earlier on my linux system whenever i tried to run the noscript but I then made some changes and stopped seeing the error message but now when i run the noscript i don't get any response at all. I'm not sure if this is a problem with the noscript or the system

The noscript I'm trying to run(from a course on yt)

```
!/bin/bash

for ip in `seq 1 254` ; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

./ipsweep.sh 192.168.4

https://redd.it/1fcwwha
@r_bash
echo $?

Hi to all,

I know that with the command "echo $?" I get the last command state.

But what about if I would ike to see the state of a command prior to the last one in bash history?

Does anybody know?

Thanks!

Vassari

https://redd.it/1fd5yar
@r_bash
what is the "user id"?

hello, i'm trying to understand the "whoami" command, and it says in the man page

"Print the user name associated with the current effective user ID."

what is the user id?

thank you

https://redd.it/1fdfoqn
@r_bash
Can't use tee, but echo works

Hey all,

I am trying to export a GPIO pin, so I can set the level.

If I do:

>echo 362 > /sys/class/gpio/export

No issues.

However, doing:

echo "362" | sudo tee /sys/class/gpio/export

3[ 192.027364] export_store: invalid GPIO 3
6[ 192.031368] export_store: invalid GPIO 6
2[ 192.035549] export_store: invalid GPIO 2

So it's writing them separately, is this expected?

I can get around that by just passing the command to bash by doing:

sudo sh -c "echo 362 > /sys/class/gpio/export"

And this works.

However, it's interesting I see the tee approach done quite a bit online, but it doesn't seem to work for me. Anyone have any ideas?

https://redd.it/1fdv3k7
@r_bash
Script with Watch command shows unwanted characters ?

Hi,

I have a bash noscript that gives the below out.

SERVICE MNXT STATUS
enodebl2 [ RUNNING ]
l1run.sh RUNNING
l1appnbiot.sh [ STOPPED ]

When the noscript is run with `watch` command, the output show the below characters.

***** SERVICE MNXT STATUS ***** enodeb
l2 ^[1;32m RUNNING ^[0m l1run.sh [ ^[1;32m RUNNING ^[0m ] l1appnbiot.sh ^[1;31m STOPPED ^[0m

What is causing this, and how to get rid of them ?

https://redd.it/1feawox
@r_bash
I have about 100 function in my .bashrc. Should I convert them into noscripts? Do they take unnecessary memory?

As per noscript. Actually I have a dedicated .bash_functions file that is sourced from .bashrc. Most of my custom functions are one liners.

Thanks.

https://redd.it/1fectb3
@r_bash
Best Practices: Multiple spaces in a $(...) for readability

Let's say that I do this in an attempt to make it easier for me to read the noscript:

foo=$(nice -n 19 ionice -c 3 \
find /foo/ -maxdepth 1 -type f -exec du -b {} + | awk '{sum += $1} END {print sum}')

In case it doesn't post the way I typed it, there's a \\ followed by a line break, then 6 spaces on the second line to make it line up with the first line.

I'm not having an errors when I run it, but is this something that I should worry about becoming an error later on? I don't use bash that often, and I dread having an error in 3 or 4 years and having no idea why.

Not that most of you can see the future... I guess I'm just asking about "best practices" O:-)

https://redd.it/1ff9eh9
@r_bash
FOSS project is looking for BASH enthusiasts

https://preview.redd.it/lgbrquf1kkod1.png?width=996&format=png&auto=webp&s=1063d8906c4a2753b09e25c44568d386373b226f

We are excited to introduce the next generation of the Armbian Config tool! This redesigned and lightweight tool is central to managing single-board computers, offering a wide range of features for both hardware-specific and general system configuration.

# Key Advantages:

* Extremely lightweight with minimal dependencies
* Redesigned from scratch for better performance and flexibility
* JSON-based menu structure with options for TUI, CLI, or API

# Quick Recap

The `armbian-config` tool has been essential for configuring single-board computers, combining our long-term expertise in Linux and the embedded world. However, the old version had become bulky and difficult to maintain, prompting us to redesign it from the ground up. This new version offers better performance, flexibility, and robustness. We’re calling on the community to help test and complete it before the upcoming release!

You can help by installing the developer version for testing:

echo "deb [signed-by=/usr/share/keyrings/armbian.gpg] stable main" \
| sudo tee /etc/apt/sources.list.d/armbian-development.list > /dev/null
sudo apt update
sudo apt install armbian-configng
https://armbian.github.io/configng

Then, execute:

sudo armbian-configng

Check help with:

sudo bin/armbian-configng --help

*Note: This is a developer version meant for testing purposes only.*

# Users: We Need Your Feedback!

This tool is not yet production-ready, and we expect issues to arise. We encourage you to submit bug reports and feature requests as you encounter them. Our team will address these based on priority and feasibility.

* [**Submit Bug Report**](https://github.com/armbian/configng/issues/new?assignees=&labels=Bug&projects=&template=1-bugreport.yml&noscript=%5BBug%5D%3A+)
* [**Request a Feature**](https://github.com/armbian/configng/issues/new?assignees=&labels=Feature+Request&projects=&template=2-feature.yml&noscript=%5BFeature+Request%5D%3A+)

# Developers: Show your talent!

We’re looking for developers to contribute to this project! If you have skills in application design, function development, or code improvement, we’d love to have your input. This new tool has been completely redesigned, so it’s more than just copy-pasting from the old `armbian-config`.

As a token of our appreciation, contributors of non-trivial code will be entered into a draw to [win a mini PC or a high-end desktop workstation](https://www.armbian.com/newsflash/armbian-khadas-are-rewarding-contributors/). Stick around to help maintain the tool, and we can even discuss monthly compensation.

Head over to GitHub to contribute: [https://github.com/armbian/configng](https://github.com/armbian/configng)
Propose changes by opening a pull request!

Thank you for your support!

https://redd.it/1fftcpo
@r_bash
Guys, I need help with this assignment. No chat GPT
https://redd.it/1fgqcgg
@r_bash
If you pipe a list of files, what bash command do you pipe it to, for it to move those files to another directory?

E.g. ls | mv ... what?

https://redd.it/1fgtg88
@r_bash
Why is the output getting mixed up? I've done tons of troubleshooting but nothing has worked. I followed a noscript from a textbook so I expected it to just function, and not reverse the order of the numbers. I can tell it has to do with the third period but can't tell why or how.

https://redd.it/1fh13d2
@r_bash
How to check if background while loop exits early?

I have a bluetooth dmenu noscript that enables the service then prompts me to select devices to connect. I have my devices auto-connect when service is enabled. I want to make it smarter--when a device connects within 3 seconds (they usually auto-connect practically instantly), do not show prompt and quit the noscript early when device connects, else show it and continue on with noscript further prompting the user.

It seems I need a while loop in the background to start counting for the timeout, right? I have something like this:

# ... code to enable bluetooth

# background while loop
declare -i i=0 timeout=3

# quit loop early if there are connected devices
while (( i <= timeout )) || (( ${#devices_connected[@]} )); do
i+=1
sleep 1
done &

pid=$!

# ... code to get `devices_connected`, expected to run as soon as
# background while loop starts so it can start seeing connected devices

if wait "$pid" && (( i < 2)); then
echo "exit now"
exit
else
echo "continue with noscript"
fi

Is this even the right approach? The code doesn't work because `i` is modified within while loop so it value is not kept.

https://redd.it/1fh5r93
@r_bash
I need help I don't know why /home/minecraft doesn't include all the files

#!/bin/bash



# Defining variables

PROJECT_ID="alexuni2"

VM_NAME="minecraft-server"

DISK_NAME="minecraft-disk2"

IP_NAME="mc-server-ip"

NETWORK_NAME="minecraft-network"

REGION="us-central1"

ZONE="us-central1-a"

BUCKET_NAME="minecraft-backup"

MINECRAFT_DIRECTORY="/home/minecraft"

SERVER_JAR_URL="https://launcher.mojang.com/v1/objects/d0d0fe2b1dc6ab4c65554cb734270872b72dadd6/server.jar"



# Set the project ID for gcloud commands

gcloud config set project $PROJECT_ID



# Step 1: Create the VPC with automatic subnetting

echo "Creating VPC with automatic subnetting..."

gcloud compute networks create $NETWORK_NAME --subnet-mode=auto --project=$PROJECT_ID



echo "Creating Cloud Storage Bucket..."

gsutil mb -l $REGION -p $PROJECT_ID gs://$BUCKET_NAME/



# Step 2: Create a static external IP address

echo "Creating static external IP..."

gcloud compute addresses create $IP_NAME --region=$REGION --project=$PROJECT_ID



# Step 3: Create the VM Instance with a blank disk and external static IP

echo "Creating VM instance."

gcloud compute instances create $VM_NAME \\

--zone=$ZONE \\

--machine-type=e2-medium \\

--network=$NETWORK_NAME \\

--tags=minecraft-server \\

--boot-disk-size=10GB \\

--boot-disk-type=pd-ssd \\

--boot-disk-device-name=$DISK_NAME \\

--address=$IP_NAME \\

--no-service-account \\

--no-scopes \\

--metadata=startup-noscript-url=https://storage.googleapis.com/cloud-training/archinfra/mcserver/startup.sh,\\

shutdown-noscript-url=https://storage.googleapis.com/cloud-training/archinfra/mcserver/shutdown.sh \\



echo "Attaching additional 50 GB SSD disk..."

gcloud compute instances attach-disk $VM_NAME \\

--disk=$DISK_NAME \\

--zone=$ZONE \\

--project=$PROJECT_ID



# Step 4: Create the Firewall Rules

echo "Creating firewall rules..."

gcloud compute firewall-rules create minecraft-allow-ssh \\

--direction=INGRESS \\

--priority=1000 \\

--network=$NETWORK_NAME \\

--action=ALLOW \\

--rules=tcp:22 \\

--source-ranges=0.0.0.0/0 \\

--project=$PROJECT_ID



gcloud compute firewall-rules create minecraft-allow-internal \\

--direction=INGRESS \\

--priority=1000 \\

--network=$NETWORK_NAME \\

--action=ALLOW \\

--rules=all \\

--source-ranges=10.128.0.0/20 \\

--project=$PROJECT_ID



gcloud compute firewall-rules create minecraft-allow-icmp \\

--direction=INGRESS \\

--priority=1000 \\

--network=$NETWORK_NAME \\

--action=ALLOW \\

--rules=icmp \\

--source-ranges=0.0.0.0/0 \\

--project=$PROJECT_ID



gcloud compute firewall-rules create minecraft-rule \\

--direction=INGRESS \\

--priority=1000 \\

--network=$NETWORK_NAME \\

--action=ALLOW \\

--rules=tcp:25565 \\

--source-ranges=0.0.0.0/0 \\

--target-tags=minecraft-server \\

--project=$PROJECT_ID



# Step 5: SSH into the VM

echo "SSH into VM: gcloud compute ssh $VM_NAME --zone=$ZONE"



# Step 6: Format and Mount the Disk

echo "Mounting and formatting disk..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-$DISK_NAME"

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo mkdir -p $MINECRAFT_DIRECTORY"

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo mount -o discard,defaults /dev/disk/by-id/google-$DISK_NAME $MINECRAFT_DIRECTORY"



# Verify the disk is mounted

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="df -h"



# Step 7: Install Java, Screen, and Other Packages

echo "Installing Java, screen, and wget..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo apt-get update && sudo apt-get install -y openjdk-11-jre-headless screen wget"



# Step 8: Download Minecraft Server JAR

echo "Downloading Minecraft server JAR..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="cd $MINECRAFT_DIRECTORY && sudo wget $SERVER_JAR_URL"



# Step 9:
init the server

echo "initialize the server..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="cd $MINECRAFT_DIRECTORY && sudo java -Xmx1024M -Xms1024M -jar server.jar nogui && \\

sudo chmod 777 eula.txt && \\

echo "eula=true" | sudo cat > eula.txt && \\"



# Step 10: Start the Minecraft Server in Screen

echo "Starting Minecraft server in a screen session..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="cd $MINECRAFT_DIRECTORY && sudo screen -dmS mcs java -Xmx1024M -Xms1024M -jar server.jar nogui && \\

sudo screen -d mcs && \\

sudo screen -r mcs"





# Step 11: Create Backup Script

echo "Creating backup noscript..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo tee $MINECRAFT_DIRECTORY/backup.sh > /dev/null <<EOL

#!/bin/bash

screen -r mcs -X stuff '/save-all\\n/save-off\\n'

/usr/bin/gsutil cp -R ${BASH_SOURCE%/*}/world gs://$BUCKET_NAME-minecraft-backup/\\$(date '+%Y%m%d-%H%M%S')-world

screen -r mcs -X stuff '/save-on\\n'

EOL"



# Step 12: Make the Backup Script Executable

echo "Making backup noscript executable..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="sudo chmod +x $MINECRAFT_DIRECTORY/backup.sh"



# Step 13: Schedule Backup to Run Every 8 Hours

echo "Scheduling backups every 8 hours..."

gcloud compute ssh $VM_NAME --zone=$ZONE --project=$PROJECT_ID --command="(crontab -l 2>/dev/null; echo '0 */8 * * * $MINECRAFT_DIRECTORY/backup.sh') | crontab -"



echo "Minecraft server setup complete. Network, VM, disk, firewall, and backups are configured."



https://redd.it/1fhvj56
@r_bash
Condition to remove ANSI characters in case of commands following a "|"

In my noscript I have some options that show colored messages.

If I prefix these with "> text.txt" or ">> text.txt" or a "| less" (by the way "less" is already included in these options), the output will also show the ANSI codes used.

I have already experimented with a filter using "sed", but who will unknowingly use the above symbols and commands, how will they have a "clean" output?

Is there a way to let the noscript know that one of the above characters or commands is in use?

https://redd.it/1fi9knc
@r_bash
Bash noscript cannot run JavaScript file with top level await

I am trying to make a systemd timer that runs a noscript every 5 minutes for notifications to my phone based on an api, and I have successfully checked that the timer and the noscript work separately. For some reason, though, whenever I have the systemd timer point to the noscript.sh and run it, and if I do sudo systemctl status noscript.service, it provides an error: await is a reserved word. I suspect this is because it is a top level await, which for some reason bash can't do? The code it runs in the file is node ./noscript.js, and I have debugged everything I can think of. Even trying to put all of my code in an await function and then doing a then catch on it doesn't work.
Any help would be greatly appreciated, and if this is the wrong subreddit please let me know!

https://redd.it/1fij8mc
@r_bash
Dot files management and bashrc for different servers

So, I am trying to use gnu stow to install my dotfiles. This seems to work for most of my config, except for the .bashrc. Here I work with multiple servers and also with my laptop. Thus, I cannot use the same .bashrc for all of them. I am thinking of using multiple files like

.bashrc_s1
.bashrc_s2
...


and some sort of master bashrc with:

if [[ "$(hostname)" == "s1" ]];then
source .bashrc_s1
...
fi


is this a good approach? What have you out there tried and got it to work?

https://redd.it/1fio66d
@r_bash