How to redirect stdin from an Interactive program to a File
I'm hoping to redirect the inpit of the python repl to a file
For example; would it be possible to set
my first attempt was doing
and then
The issue there is it's grabbing only the output from the repl and the output of the interpreter, now the i/o stream of reading the commands.
Any ideas are welcome..
https://redd.it/xrsdjv
@r_bash
I'm hoping to redirect the inpit of the python repl to a file
For example; would it be possible to set
exec 3>&1 file denoscriptor somehow pipe stdin from the python repl to stdout into a log file?my first attempt was doing
exec 3>&1and then
python 2>&1 | >&3 tee -a SomeFile.logThe issue there is it's grabbing only the output from the repl and the output of the interpreter, now the i/o stream of reading the commands.
Any ideas are welcome..
https://redd.it/xrsdjv
@r_bash
reddit
How to redirect stdin from an Interactive program to a File
I'm hoping to redirect the inpit of the python repl to a file For example; would it be possible to set `exec 3>&1` file denoscriptor somehow pipe...
Whiptail --menu help
​
Can someone explain what ' 3\>&1 1\>&2 2\>&3 ' means
https://redd.it/xrzvxt
@r_bash
​
Can someone explain what ' 3\>&1 1\>&2 2\>&3 ' means
https://redd.it/xrzvxt
@r_bash
FuzzyFind with Array
### I want to pipe the content of a directory(both files and directories) to fzf
this is my attempt so far. it's not functional
### the expected output:
fzf the content of the specified directory
### the current ouput:
fzf the content of the current directory===>$PWD
Thx in advance
https://redd.it/xs1egg
@r_bash
### I want to pipe the content of a directory(both files and directories) to fzf
this is my attempt so far. it's not functional
#!/usr/bin/env bash
content_dir="$HOME/DevRepo"
### this is my array
arr=$(ls "$content_dir")
fzf --preview "printf '%s\n' \"\${arr[{n}]}\""
### the expected output:
fzf the content of the specified directory
### the current ouput:
fzf the content of the current directory===>$PWD
Thx in advance
https://redd.it/xs1egg
@r_bash
reddit
FuzzyFind with Array
### I want to pipe the content of a directory(both files and directories) to fzf this is my attempt so far. it's not functional...
Bash Newbie - How to do a foreach in bash?
Hello! This is my first time writing in bash, and I am having troubles understanding something basic. I would like to use a for each on a .txt file that consists of multiple commands to check machine information.
​
In powershell, I would do something like this:
​
$commandlist = get-content 'c:\commandlist.txt'
foreach ($command in $commandlist)
{Write-host "Running the command $command"
invoke-expression $command}
In bash, I am doing something like this:
#!/bin/bash
commandlist='commands.txt'
for command in $commandlist
do
echo "Running the command $command"
bash "$command"
done
however my output is like this:
Doing command commands.txt
MyHostName
IFconfigDetails
It seems that I am not looping correctly. What am I missing?
​
I also read about this and this seems to work:
#!/bin/bash
cat commands.txt | while read -r line
do
echo "$line"
"$line"
done
Is this the only way I can do a foreach on a .txt file? thank you!
thank you in advance!
​
PS. commands.txt contains only 2 lines
Hostname
IFconfig
https://redd.it/xs47yz
@r_bash
Hello! This is my first time writing in bash, and I am having troubles understanding something basic. I would like to use a for each on a .txt file that consists of multiple commands to check machine information.
​
In powershell, I would do something like this:
​
$commandlist = get-content 'c:\commandlist.txt'
foreach ($command in $commandlist)
{Write-host "Running the command $command"
invoke-expression $command}
In bash, I am doing something like this:
#!/bin/bash
commandlist='commands.txt'
for command in $commandlist
do
echo "Running the command $command"
bash "$command"
done
however my output is like this:
Doing command commands.txt
MyHostName
IFconfigDetails
It seems that I am not looping correctly. What am I missing?
​
I also read about this and this seems to work:
#!/bin/bash
cat commands.txt | while read -r line
do
echo "$line"
"$line"
done
Is this the only way I can do a foreach on a .txt file? thank you!
thank you in advance!
​
PS. commands.txt contains only 2 lines
Hostname
IFconfig
https://redd.it/xs47yz
@r_bash
reddit
Bash Newbie - How to do a foreach in bash?
Hello! This is my first time writing in bash, and I am having troubles understanding something basic. I would like to use a for each on a .txt...
Reversing order of input
How do I do this? I want to take an input say, "Input Number One", and print back "One Number Input". I've been able to reverse the entire thing, but not the order. All online I've seen talks about reversing files.
https://redd.it/xs7dym
@r_bash
How do I do this? I want to take an input say, "Input Number One", and print back "One Number Input". I've been able to reverse the entire thing, but not the order. All online I've seen talks about reversing files.
https://redd.it/xs7dym
@r_bash
reddit
Reversing order of input
How do I do this? I want to take an input say, "Input Number One", and print back "One Number Input". I've been able to reverse the entire thing,...
trying to find files by time signature
I want to find out the last two files added to the folder.
The files are .csv files that I scp from a remote machine to local.
I want to find and move these two files to a new folder.
To find the files I issued this command `:\~$ find . -maxdepth 1 -newermt "2022-09-30" | grep .csv | ls -al --time-style=+%D`
I expected the first pipe to print only the .csv files (which it did) then I added the second pipe thinking it will list the csv files according to their date. Which did not happen. What happend was the listing of all files and folders together with their date.
What did I do wrong?
​
edit
This did succeed
https://redd.it/xs6yul
@r_bash
I want to find out the last two files added to the folder.
The files are .csv files that I scp from a remote machine to local.
I want to find and move these two files to a new folder.
To find the files I issued this command `:\~$ find . -maxdepth 1 -newermt "2022-09-30" | grep .csv | ls -al --time-style=+%D`
I expected the first pipe to print only the .csv files (which it did) then I added the second pipe thinking it will list the csv files according to their date. Which did not happen. What happend was the listing of all files and folders together with their date.
What did I do wrong?
​
edit
This did succeed
find . -maxdepth 1 -newermt "2022-09-30" | ls -al --time-style=+%D | grep .csvhttps://redd.it/xs6yul
@r_bash
reddit
trying to find files by time signature
I want to find out the last two files added to the folder. The files are .csv files that I scp from a remote machine to local. I want to find...
Piping multiple commands
So while I am interested in my concrete issue, I am also interested in how I can pipe multiple functions into one command in bash. What I want to do is compare two sorted files using
If the lines are not sorted I can easilly do this using
git --no-pager diff --no-index oldfile.txt newfile.txt
However, when my files needs to be sorted, I run into issues. I do not know if / how I can properly pipe them into
git --no-pager diff --no-index | sort oldfile.txt | sort newfile.txt
https://redd.it/xsdlqt
@r_bash
So while I am interested in my concrete issue, I am also interested in how I can pipe multiple functions into one command in bash. What I want to do is compare two sorted files using
git diffIf the lines are not sorted I can easilly do this using
git --no-pager diff --no-index oldfile.txt newfile.txt
However, when my files needs to be sorted, I run into issues. I do not know if / how I can properly pipe them into
git diff without saving them first as tempfiles.git --no-pager diff --no-index | sort oldfile.txt | sort newfile.txt
https://redd.it/xsdlqt
@r_bash
reddit
Piping multiple commands
So while I am interested in my concrete issue, I am also interested in how I can pipe multiple functions into one command in bash. What I want to...
Working with Indexed Arrays
# Introduction
I decided to write this to share what I've learned about arrays in bash. It is not complete, and I expect to learn a lot, if I get any replies to this.
I also fully expect to screw up the formatting, and will probably be sweating profusely while trying to fix it. Please bear with me.
# What are Arrays
Arrays are a method for storing lists and dictionaries of information. There are two types of arrays supported by Bash, indexed and associative arrays. Indexed arrays have numeric indices and values associated with the indices. Associative arrays have key/value pairs. I'll be focusing on indexed arrays here.
With indexed arrays, you can store data in the array, iterate over the array, and operate on the each element in the array. For example:
cdickbag@dickship:~$ indarr=(apple orange banana)
cdickbag@dickship:~$ for fruit in "${indarr@}"; do echo "${fruit}"; done
apple
orange
banana
cdickbag@dickship:~$ echo "${indarr[0]}"
apple
cdickbag@dickship:~$ echo "${indarr1}"
orange
cdickbag@dickship:~$ echo "${indarr[2]}"
banana
This becomes more useful when you want to do things like iterate over text from a file, pattern match, and maybe go back to the previous line which contains unknown text, modify it, then write the contents to a file. If you work with lists of almost anything, arrays can be helpful to you.
# Finding Your Version of Bash
There are lots of different versions of bash in the wild. For example, macOS ships with bash 3.2.7, released in 2007. It lacks very handy features, like mapfile/readarray. Knowing your bash version is important to determine which features are available to you.
Find the bash version in your running shell.
echo $BASHVERSION
Find the version of bash in your path.
bash --version
# Creating Indexed Arrays
There are a variety of ways to create indexed arrays.
## Manually
Declare an array.
cdickbag@dickship:~$ declare -a indarr
Simply start assigning values. You don't have to use `declare` to do this. Bash is very forgiving in that way.
cdickbag@dickship:~$ indarr=(apple orange banana)
If you have long lists you want to populate manually, you can reformat them so they're easier to read.
indarr=(
apple
orange
banana
)
## Automatically
Creating arrays by hand is tedious if you have a lot of objects. For example, if you want to pull data from a database, and store it in an array for processing, or want to read a text file into memory to process line by line, you would want to have some way to automatically read that information into an array.
### Using Loops and mapfile/readarray
In this example, I'll use a text file called `input.txt` with the following text.
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Reading a file into an array is easiest with mapfile/readarray. From the GNU Bash Reference Manual:
> Read lines from the standard input into the indexed array variable array, or from file denoscriptor fd if the -u option is supplied. The variable MAPFILE is the default array.
cdickbag@dickship:~$ mapfile -t indarr < input.txt
In older shells, such as bash 3.2.7, your options are more limited. Mapfile isn't available, so you need to do something else. A `while` loop works well here. Note the use of `+=`, which adds an element to an array. The use of parentheses is also important. Without them, `+=` concatenates a string to a variable.
cdickbag@dickship:~$ while read line; do indarr+=("${line}"); done < input.txt
But what if you want to populate an array from a process instead of a file? Process substitution makes this easy. Process substitution allows a process's input or output to be referred to using a filename.
# Introduction
I decided to write this to share what I've learned about arrays in bash. It is not complete, and I expect to learn a lot, if I get any replies to this.
I also fully expect to screw up the formatting, and will probably be sweating profusely while trying to fix it. Please bear with me.
# What are Arrays
Arrays are a method for storing lists and dictionaries of information. There are two types of arrays supported by Bash, indexed and associative arrays. Indexed arrays have numeric indices and values associated with the indices. Associative arrays have key/value pairs. I'll be focusing on indexed arrays here.
With indexed arrays, you can store data in the array, iterate over the array, and operate on the each element in the array. For example:
cdickbag@dickship:~$ indarr=(apple orange banana)
cdickbag@dickship:~$ for fruit in "${indarr@}"; do echo "${fruit}"; done
apple
orange
banana
cdickbag@dickship:~$ echo "${indarr[0]}"
apple
cdickbag@dickship:~$ echo "${indarr1}"
orange
cdickbag@dickship:~$ echo "${indarr[2]}"
banana
This becomes more useful when you want to do things like iterate over text from a file, pattern match, and maybe go back to the previous line which contains unknown text, modify it, then write the contents to a file. If you work with lists of almost anything, arrays can be helpful to you.
# Finding Your Version of Bash
There are lots of different versions of bash in the wild. For example, macOS ships with bash 3.2.7, released in 2007. It lacks very handy features, like mapfile/readarray. Knowing your bash version is important to determine which features are available to you.
Find the bash version in your running shell.
echo $BASHVERSION
Find the version of bash in your path.
bash --version
# Creating Indexed Arrays
There are a variety of ways to create indexed arrays.
## Manually
Declare an array.
cdickbag@dickship:~$ declare -a indarr
Simply start assigning values. You don't have to use `declare` to do this. Bash is very forgiving in that way.
cdickbag@dickship:~$ indarr=(apple orange banana)
If you have long lists you want to populate manually, you can reformat them so they're easier to read.
indarr=(
apple
orange
banana
)
## Automatically
Creating arrays by hand is tedious if you have a lot of objects. For example, if you want to pull data from a database, and store it in an array for processing, or want to read a text file into memory to process line by line, you would want to have some way to automatically read that information into an array.
### Using Loops and mapfile/readarray
In this example, I'll use a text file called `input.txt` with the following text.
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Reading a file into an array is easiest with mapfile/readarray. From the GNU Bash Reference Manual:
> Read lines from the standard input into the indexed array variable array, or from file denoscriptor fd if the -u option is supplied. The variable MAPFILE is the default array.
cdickbag@dickship:~$ mapfile -t indarr < input.txt
In older shells, such as bash 3.2.7, your options are more limited. Mapfile isn't available, so you need to do something else. A `while` loop works well here. Note the use of `+=`, which adds an element to an array. The use of parentheses is also important. Without them, `+=` concatenates a string to a variable.
cdickbag@dickship:~$ while read line; do indarr+=("${line}"); done < input.txt
But what if you want to populate an array from a process instead of a file? Process substitution makes this easy. Process substitution allows a process's input or output to be referred to using a filename.
/dev/fd/63 is where bash will read from. Our input is the command ip addr show.www.gnu.org
Bash Builtins (Bash Reference Manual)
Next: Modifying Shell Behavior, Previous: Bourne Shell Builtins, Up: Shell Builtin Commands [Contents][Index]
cdickbag@dickship:~$ mapfile -t indarr < <(ip addr show)
# Working with Arrays
Now that we've gone over a few ways to feed data into arrays, let's go over basic usage.
Print each element of an array by iterating over it with a `for` loop.
cdickbag@dickship:~$ for i in "${indarr@}"; do echo "${i}"; done
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Print the number of elements in the array.
cdickbag@dickship:~$ echo "${#indarr@}"
3
Print the indices of the array.
cdickbag@dickship:~$ echo "${!indarr[@]}"
0 1 2
Print a specific element of the array by index.
cdickbag@dickship:~$ echo "${indarr0}"
lineonehasunderscores
cdickbag@dickship:~$ echo "${indarr1}"
line two has multiple words separated by spaces
cdickbag@dickship:~$ echo "${indarr[2]}"
linethreeisoneword
Append an element to the array.
cdickbag@dickship:~$ indarr+=(line-four-has-dashes)
Delete an element from the array.
cdickbag@dickship:~$ unset 'indarr[3]'
# Pitfalls
I often see people creating arrays using [command substitution](https://www.gnu.org/software/bash/manual/htmlnode/Command-Substitution.html) in one of two ways.
cdickbag@dickship:~$ indarr=$(cat input.txt)
This creates a variable which we *can* iterate over, but it doesn't do what we expect. What we expect is that we have one line of text per index in the array. What we find when we try to treat it as an array is that there is only one element in the array. We can demonstrate this by printing the length, and the indices themselves.
cdickbag@dickship:~$ echo "${#indarr@}"
1
cdickbag@dickship:~$ echo "${!indarr[@]}"
0
One element, one index. In order to visualize what's contained in the variable, we can do the following.
cdickbag@dickship:~$ for line in "${indarr@}"; do echo "${line}"; echo ""; done
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Where we would expect a line of underscores between each individual line, we instead only have a line of underscores at the very bottom. This is consistent with what we saw when printing the number of elements, and the indices themselves.
There is a way to iterate over it like an array. Don't treat it like an array.
cdickbag@dickship:~$ for line in ${indarr}; do echo "${line}"; echo ""; done
lineonehasunderscores
line
two
has
multiple
words
separated
by
spaces
linethreeisoneword
The problem with this method becomes apparent immediately. Line two, which had spaces between words, is now being split on space. This is problematic if we need individual lines to maintain integrity for any particular reason, such as testing lines with spaces for the presence of a pattern.
The second method has similar issues, but creates an array with indices. This is better.
cdickbag@dickship:~$ indarr=($(cat input.txt))
cdickbag@dickship:~$ echo "${#indarr[@]}"
10
cdickbag@dickship:~$ echo "${!indarr@}"
0 1 2 3 4 5 6 7 8 9
The problem is already evident. There should be three lines, therefore indices 0-2, but we instead see indices 0-9. Are we splitting on space again?
cdickbag@dickship:~$ for line in "${indarr[@]}"; do echo "${line}"; echo "Text between lines"; done
lineonehasunderscores
Text between lines
line
Text between lines
two
Text between lines
has
Text between lines
multiple
Text between lines
words
Text between lines
separated
Text between lines
by
Text between lines
spaces
Text between lines
linethreeisoneword
Text between lines
We are. Individual elements can be printed, which is an improvement over the previous method using
# Working with Arrays
Now that we've gone over a few ways to feed data into arrays, let's go over basic usage.
Print each element of an array by iterating over it with a `for` loop.
cdickbag@dickship:~$ for i in "${indarr@}"; do echo "${i}"; done
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Print the number of elements in the array.
cdickbag@dickship:~$ echo "${#indarr@}"
3
Print the indices of the array.
cdickbag@dickship:~$ echo "${!indarr[@]}"
0 1 2
Print a specific element of the array by index.
cdickbag@dickship:~$ echo "${indarr0}"
lineonehasunderscores
cdickbag@dickship:~$ echo "${indarr1}"
line two has multiple words separated by spaces
cdickbag@dickship:~$ echo "${indarr[2]}"
linethreeisoneword
Append an element to the array.
cdickbag@dickship:~$ indarr+=(line-four-has-dashes)
Delete an element from the array.
cdickbag@dickship:~$ unset 'indarr[3]'
# Pitfalls
I often see people creating arrays using [command substitution](https://www.gnu.org/software/bash/manual/htmlnode/Command-Substitution.html) in one of two ways.
cdickbag@dickship:~$ indarr=$(cat input.txt)
This creates a variable which we *can* iterate over, but it doesn't do what we expect. What we expect is that we have one line of text per index in the array. What we find when we try to treat it as an array is that there is only one element in the array. We can demonstrate this by printing the length, and the indices themselves.
cdickbag@dickship:~$ echo "${#indarr@}"
1
cdickbag@dickship:~$ echo "${!indarr[@]}"
0
One element, one index. In order to visualize what's contained in the variable, we can do the following.
cdickbag@dickship:~$ for line in "${indarr@}"; do echo "${line}"; echo ""; done
lineonehasunderscores
line two has multiple words separated by spaces
linethreeisoneword
Where we would expect a line of underscores between each individual line, we instead only have a line of underscores at the very bottom. This is consistent with what we saw when printing the number of elements, and the indices themselves.
There is a way to iterate over it like an array. Don't treat it like an array.
cdickbag@dickship:~$ for line in ${indarr}; do echo "${line}"; echo ""; done
lineonehasunderscores
line
two
has
multiple
words
separated
by
spaces
linethreeisoneword
The problem with this method becomes apparent immediately. Line two, which had spaces between words, is now being split on space. This is problematic if we need individual lines to maintain integrity for any particular reason, such as testing lines with spaces for the presence of a pattern.
The second method has similar issues, but creates an array with indices. This is better.
cdickbag@dickship:~$ indarr=($(cat input.txt))
cdickbag@dickship:~$ echo "${#indarr[@]}"
10
cdickbag@dickship:~$ echo "${!indarr@}"
0 1 2 3 4 5 6 7 8 9
The problem is already evident. There should be three lines, therefore indices 0-2, but we instead see indices 0-9. Are we splitting on space again?
cdickbag@dickship:~$ for line in "${indarr[@]}"; do echo "${line}"; echo "Text between lines"; done
lineonehasunderscores
Text between lines
line
Text between lines
two
Text between lines
has
Text between lines
multiple
Text between lines
words
Text between lines
separated
Text between lines
by
Text between lines
spaces
Text between lines
linethreeisoneword
Text between lines
We are. Individual elements can be printed, which is an improvement over the previous method using
command substitution, but we still have issues splitting on space.
The two command substitution methods can work, but you have to be aware of their limitations, and how to handle them when you use them. Generally speaking, if you want a list of items, use an array, and be aware of what method you choose to populate it.
https://redd.it/xsp3m2
@r_bash
The two command substitution methods can work, but you have to be aware of their limitations, and how to handle them when you use them. Generally speaking, if you want a list of items, use an array, and be aware of what method you choose to populate it.
https://redd.it/xsp3m2
@r_bash
reddit
Working with Indexed Arrays
# Introduction I decided to write this to share what I've learned about arrays in bash. It is not complete, and I expect to learn a lot, if I get...
Check if variable has "#" as its first character
Hello Bash Masters!
​
First of all, I would like to thank everyone for assisting me on my previous post.
As advised, I am a super bash and linux noob and, this is literally the first time for me to write a bash noscript. I apologize in advance if my question/s sound silly.
​
I am working on a noscript that runs the commands saved on a .txt file - Check if the command works if yes, output this into a .txt file.
​
I currently have this:
#!/usr/bin/bash
#CREATE OUTPUT DIRECTORY IF DIRECTORY DOES NOT EXIST
mkdir -p /datacollect/commandlist
#CHECK THE TXT FILE FOR WHITESPACES
sed -i '/^$/d' commandslist.txt
#TEXT FORMAT
GREEN='\0330;32m'
RED='\033[0;31m'
NC='\033[0m'
#FOREACH LINE IN COMMANDS.TXT DO THE FOLLOWING
while read -r line; do
echo "${GREEN}Running Command $line${NC}"
eval "$line"
if [ "$?" -ne 0
then
echo "${RED}The command $line did not work, the command might not exist or is typed in incorrectly. Skipping.${NC}"
echo ""
echo ""
else
echo "$line" >> commandlist/commandlist.txt
eval "$line" >> commandlist/commandlist.txt
echo "" >> commandlist/commandlist.txt
echo "" >> commandlist/commandlist.txt
echo ""
echo ""
fi
done < commandlist.txt
I guess, all in all it is working now, aside from the previous comments about $? and the advise that I have gotten on why I should not use it. I am still testing and checking how to do this.
​
I would like to add in #Comments on my commandlist.txt to separate my commands.
commandlist.txt sample:
#SYSTEM AND HARDWARE COMMANDS
hostname
lshw
#DISK COMMANDS
lsblk
fdisk -l
#NETWORK COMMANDS
IFCONFIG
#SECURITY COMMANDS
I woulld like to try and do an if statement to filter out the output of my $line variable.
If $line does not contain "#", run the command.
For some reason, I am unable to make it work. I have tried the options found here:
https://stackoverflow.com/questions/28514484/bash-how-to-check-if-a-string-starts-with
and I keep on getting errors. here is a sample of what I was trying to do:
line='#foo'
[ "$line" == "#"* ] && echo "$line starts with #"
#foo starts with #
and I get the error: test.sh: 2: test.sh: [[: not found
​
Trying several other if statements found on the afformentioned link, would result in a similar error.
​
What am I doing wrong?
​
Thank you in advance!
https://redd.it/xssp80
@r_bash
Hello Bash Masters!
​
First of all, I would like to thank everyone for assisting me on my previous post.
As advised, I am a super bash and linux noob and, this is literally the first time for me to write a bash noscript. I apologize in advance if my question/s sound silly.
​
I am working on a noscript that runs the commands saved on a .txt file - Check if the command works if yes, output this into a .txt file.
​
I currently have this:
#!/usr/bin/bash
#CREATE OUTPUT DIRECTORY IF DIRECTORY DOES NOT EXIST
mkdir -p /datacollect/commandlist
#CHECK THE TXT FILE FOR WHITESPACES
sed -i '/^$/d' commandslist.txt
#TEXT FORMAT
GREEN='\0330;32m'
RED='\033[0;31m'
NC='\033[0m'
#FOREACH LINE IN COMMANDS.TXT DO THE FOLLOWING
while read -r line; do
echo "${GREEN}Running Command $line${NC}"
eval "$line"
if [ "$?" -ne 0
then
echo "${RED}The command $line did not work, the command might not exist or is typed in incorrectly. Skipping.${NC}"
echo ""
echo ""
else
echo "$line" >> commandlist/commandlist.txt
eval "$line" >> commandlist/commandlist.txt
echo "" >> commandlist/commandlist.txt
echo "" >> commandlist/commandlist.txt
echo ""
echo ""
fi
done < commandlist.txt
I guess, all in all it is working now, aside from the previous comments about $? and the advise that I have gotten on why I should not use it. I am still testing and checking how to do this.
​
I would like to add in #Comments on my commandlist.txt to separate my commands.
commandlist.txt sample:
#SYSTEM AND HARDWARE COMMANDS
hostname
lshw
#DISK COMMANDS
lsblk
fdisk -l
#NETWORK COMMANDS
IFCONFIG
#SECURITY COMMANDS
I woulld like to try and do an if statement to filter out the output of my $line variable.
If $line does not contain "#", run the command.
For some reason, I am unable to make it work. I have tried the options found here:
https://stackoverflow.com/questions/28514484/bash-how-to-check-if-a-string-starts-with
and I keep on getting errors. here is a sample of what I was trying to do:
line='#foo'
[ "$line" == "#"* ] && echo "$line starts with #"
#foo starts with #
and I get the error: test.sh: 2: test.sh: [[: not found
​
Trying several other if statements found on the afformentioned link, would result in a similar error.
​
What am I doing wrong?
​
Thank you in advance!
https://redd.it/xssp80
@r_bash
Stack Overflow
bash: how to check if a string starts with '#'?
In bash I need to check if a string starts with '#' sign. How do I do that?
This is my take --
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
I want to run this noscript over a f...
This is my take --
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
I want to run this noscript over a f...
Bash module system
Hi, I'm writing my 1st sh noscript, and it's getting bigger and bigger how I add new features to it, so my question is, do we write all logic and commands inside one file or we have some module system? bdw I tried to find anything related to modules in bash on google but without much results.
Thanks in advance!
https://redd.it/xsvoip
@r_bash
Hi, I'm writing my 1st sh noscript, and it's getting bigger and bigger how I add new features to it, so my question is, do we write all logic and commands inside one file or we have some module system? bdw I tried to find anything related to modules in bash on google but without much results.
Thanks in advance!
https://redd.it/xsvoip
@r_bash
reddit
Bash module system
Hi, I'm writing my 1st sh noscript, and it's getting bigger and bigger how I add new features to it, so my question is, do we write all logic and...
Reading keyboard input in the background
I have a while loop that takes a long time to execute because of some other things in it, and now I want to get keyboard input from that same loop, the problem is that using "read" only captures key presses once every 10 or so times, which I'm guessing is because those other things are taking up so much time per loop that the chances of the key being pressed while read is active are pretty low.
So now my question is, is there a way to read input in the background? Or maybe just to get the last key that was pressed, regardless of when that was?
https://redd.it/xt22w3
@r_bash
I have a while loop that takes a long time to execute because of some other things in it, and now I want to get keyboard input from that same loop, the problem is that using "read" only captures key presses once every 10 or so times, which I'm guessing is because those other things are taking up so much time per loop that the chances of the key being pressed while read is active are pretty low.
So now my question is, is there a way to read input in the background? Or maybe just to get the last key that was pressed, regardless of when that was?
https://redd.it/xt22w3
@r_bash
reddit
Reading keyboard input in the background
I have a while loop that takes a long time to execute because of some other things in it, and now I want to get keyboard input from that same...
bgit - the beginner's git noscript
Hi All. I recently took a bit of time to write up one of my first extensive bash noscripts. This was mainly an educational project for me to teach me a bit of bash, git, groff (first time writing a man page), as well as others.
You can check out the README for a more in depth explanation, but it's basically a bash noscript wrapper around basic git commands with colorized styling and devicons.
I'd be grateful and flattered if anyone tried it out and found it somewhat useful (there are far better more feature filled similar tools out there, but this works for my simple workflow, and perhaps it will work for you).
Constructive Criticism and Feedback is always welcome.
There are two repo links available (github and codeberg):
https://codeberg.org/z3rOR0ne/bgit
https://github.com/tomit4/bgit
https://redd.it/xt40e2
@r_bash
Hi All. I recently took a bit of time to write up one of my first extensive bash noscripts. This was mainly an educational project for me to teach me a bit of bash, git, groff (first time writing a man page), as well as others.
You can check out the README for a more in depth explanation, but it's basically a bash noscript wrapper around basic git commands with colorized styling and devicons.
I'd be grateful and flattered if anyone tried it out and found it somewhat useful (there are far better more feature filled similar tools out there, but this works for my simple workflow, and perhaps it will work for you).
Constructive Criticism and Feedback is always welcome.
There are two repo links available (github and codeberg):
https://codeberg.org/z3rOR0ne/bgit
https://github.com/tomit4/bgit
https://redd.it/xt40e2
@r_bash
Codeberg.org
bgit
A simple bash noscript that simplifies a standard git workflow.
Bash noscript to mail results of ping test - Security Question
Hi all, I am a newbie to Bash noscripting.
I wrote a simple noscript which will ping a host and send an email if the ping test fails.
The bash noscript uses a CURL command for accessing the SMTP server which is on gmail, however, the Google App password used in the CURL command is hard-coded in my bash noscript.
​
#!/bin/bash
sender="myemail@gmail.com"
receiver="myemail@gmail.com"
gapp="xxxxxxxxxpacy"
# read -p "Enter the subject of mail : " sub
sub="Daily Ping Test"
# if ping -c 1 123.45.67.89 &> /dev/null # Host which succeeds ping test
if ping -c 1 9.87.65.43 &> /dev/null # Host which fails ping test
then
body="Ping succeeded, but I still sent an email :)"
echo "Ping succeeded"
else
body="RED ALERT ::: Ping failed to host!"
echo "Ping failed"
fi
# curl command for accessing the smtp server
curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from $sender \
--mail-rcpt $receiver\
--user $sender:$gapp \
-T <(echo -e "From: ${sender}
To: ${receiver}
Subject:${sub}
${body}")
My question is if this is a bad design practice to hard-code this Google App password and what would be a more secure alternative for sending an email from a bash noscript (Should I host the SMTP server on my local computer instead?) -- Any advice on setting up a SMTP server locally would be appreciated if recommended. I could read in the Google App password, but that won't be useful as the goal is to run this noscript from a CRON job daily (health-checks on the servers).
Thanks in advance!
https://redd.it/xt6d9o
@r_bash
Hi all, I am a newbie to Bash noscripting.
I wrote a simple noscript which will ping a host and send an email if the ping test fails.
The bash noscript uses a CURL command for accessing the SMTP server which is on gmail, however, the Google App password used in the CURL command is hard-coded in my bash noscript.
​
#!/bin/bash
sender="myemail@gmail.com"
receiver="myemail@gmail.com"
gapp="xxxxxxxxxpacy"
# read -p "Enter the subject of mail : " sub
sub="Daily Ping Test"
# if ping -c 1 123.45.67.89 &> /dev/null # Host which succeeds ping test
if ping -c 1 9.87.65.43 &> /dev/null # Host which fails ping test
then
body="Ping succeeded, but I still sent an email :)"
echo "Ping succeeded"
else
body="RED ALERT ::: Ping failed to host!"
echo "Ping failed"
fi
# curl command for accessing the smtp server
curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from $sender \
--mail-rcpt $receiver\
--user $sender:$gapp \
-T <(echo -e "From: ${sender}
To: ${receiver}
Subject:${sub}
${body}")
My question is if this is a bad design practice to hard-code this Google App password and what would be a more secure alternative for sending an email from a bash noscript (Should I host the SMTP server on my local computer instead?) -- Any advice on setting up a SMTP server locally would be appreciated if recommended. I could read in the Google App password, but that won't be useful as the goal is to run this noscript from a CRON job daily (health-checks on the servers).
Thanks in advance!
https://redd.it/xt6d9o
@r_bash
reddit
Bash noscript to mail results of ping test - Security Question
Hi all, I am a newbie to Bash noscripting. I wrote a simple noscript which will ping a host and send an email if the ping test fails. The bash...
Running ". ~/.bashrc " and "export PATH="$HOME/.local/bin:$PATH" " at every log on
I am a Linux hobbyist and toy around with my raspberry pi at home. Sometime ago I was having issues where when I logged in the shell looked different (lacking colors, which is normal when I ssh into my raspberry pi). Through some troubleshooting I found out that I had to now run " . ~/.bashrc " and " export PATH="$HOME/.local/bin:$PATH" " every time I log in.
I tried adding these commands to my ~/.profile directory with no luck.
What am I doing wrong? Would it help if I posted screenshots?
https://redd.it/xt2imk
@r_bash
I am a Linux hobbyist and toy around with my raspberry pi at home. Sometime ago I was having issues where when I logged in the shell looked different (lacking colors, which is normal when I ssh into my raspberry pi). Through some troubleshooting I found out that I had to now run " . ~/.bashrc " and " export PATH="$HOME/.local/bin:$PATH" " every time I log in.
I tried adding these commands to my ~/.profile directory with no luck.
What am I doing wrong? Would it help if I posted screenshots?
https://redd.it/xt2imk
@r_bash
reddit
Running ". ~/.bashrc " and "export PATH="$HOME/.local/bin:$PATH" "...
I am a Linux hobbyist and toy around with my raspberry pi at home. Sometime ago I was having issues where when I logged in the shell looked...
Do anyone know why zenity doesn't work under VNC? I'm making a noscript for "proot-ed debian" (although VNC in termux-x11, zenity seems to work)
https://redd.it/xtjr57
@r_bash
https://redd.it/xtjr57
@r_bash
Help with a rename pls
When I try to rename all files and subfolders to replace every '_' (underscore) with a '-' (dash), I get a bunch of errors;
find . -type f | rename -v 's//-/g'
Can't rename ./Lifeline/Season1/LifelineS01E03.mp4 ./Lifeline/Season-1/Lifeline-S01E03.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E06.mp4 ./Lifeline/Season-1/Lifeline-S01E06.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E07.mp4 ./Lifeline/Season-1/Lifeline-S01E07.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E05.mp4 ./Lifeline/Season-1/Lifeline-S01E05.mp4: No such file or directory
When those files obviously do exist, some thing wrong with my syntax?
I can see the problem, it's replacing the \ in the subfolder name with a - which is not what I had intended.
Not sure how to correct this, ?
https://redd.it/xtreyq
@r_bash
When I try to rename all files and subfolders to replace every '_' (underscore) with a '-' (dash), I get a bunch of errors;
find . -type f | rename -v 's//-/g'
Can't rename ./Lifeline/Season1/LifelineS01E03.mp4 ./Lifeline/Season-1/Lifeline-S01E03.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E06.mp4 ./Lifeline/Season-1/Lifeline-S01E06.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E07.mp4 ./Lifeline/Season-1/Lifeline-S01E07.mp4: No such file or directory
Can't rename ./Lifeline/Season1/LifelineS01E05.mp4 ./Lifeline/Season-1/Lifeline-S01E05.mp4: No such file or directory
When those files obviously do exist, some thing wrong with my syntax?
I can see the problem, it's replacing the \ in the subfolder name with a - which is not what I had intended.
Not sure how to correct this, ?
https://redd.it/xtreyq
@r_bash
reddit
Help with a rename pls
When I try to rename all files and subfolders to replace every '\_' (underscore) with a '-' (dash), I get a bunch of errors; find . -type f |...
Simultaneous up- and download
Not a bash question, but not think it fits somewhat. I'm looking for a tool that lets me start uploading a file while it's being downloaded. The reason is that my uni's server does not allow downloads from the internet, so you have to download data you want to work with to you local machine first and then upload to the server. It would be a large speed up, if the local machine would just sort of act as a router, immediately forwarding the data upon receival. I've searched the internet a little, but wasn't able to find a technique that lets you do this. Does anyone know if someone has coded this up?
https://redd.it/xtspdc
@r_bash
Not a bash question, but not think it fits somewhat. I'm looking for a tool that lets me start uploading a file while it's being downloaded. The reason is that my uni's server does not allow downloads from the internet, so you have to download data you want to work with to you local machine first and then upload to the server. It would be a large speed up, if the local machine would just sort of act as a router, immediately forwarding the data upon receival. I've searched the internet a little, but wasn't able to find a technique that lets you do this. Does anyone know if someone has coded this up?
https://redd.it/xtspdc
@r_bash
reddit
Simultaneous up- and download
Not a bash question, but not think it fits somewhat. I'm looking for a tool that lets me start uploading a file while it's being downloaded. The...
new coder needs help. "montage: command not found"
Hi all,
I'm a newer coder. Basically all the coding I know is in relation to trying to find shortcuts for my analog photography hobby.
About a year ago I pieced together the code below thanks to this tutorial.
Using ImageMagick to Create Contact Sheets (Montage)
I used this code successfully numerous times, but its been months since I did last and now it's not working anymore. I believe i have upgrade my OS once since using it last. also, im pretty sure i upgraded to imagemagick 7 and now have deleted that and downloaded imagemagick 6 because i need "legacy commands"
Now when I enter this command into terminal I get "montage: command not found"
It's very possible i missed something simple, so please give me any ideas you can think of, even if they're elementary.
thanks so much!
https://redd.it/xtx179
@r_bash
Hi all,
I'm a newer coder. Basically all the coding I know is in relation to trying to find shortcuts for my analog photography hobby.
About a year ago I pieced together the code below thanks to this tutorial.
Using ImageMagick to Create Contact Sheets (Montage)
montage -verbose -label '%f' -font Helvetica -pointsize 10 -background '#000000' -fill 'gray' -define jpeg:size=200x200 -geometry 200x200+2+2 -auto-orient florida{001..005}.jpg JPG ~/Desktop/florida11.jpg I used this code successfully numerous times, but its been months since I did last and now it's not working anymore. I believe i have upgrade my OS once since using it last. also, im pretty sure i upgraded to imagemagick 7 and now have deleted that and downloaded imagemagick 6 because i need "legacy commands"
Now when I enter this command into terminal I get "montage: command not found"
It's very possible i missed something simple, so please give me any ideas you can think of, even if they're elementary.
thanks so much!
https://redd.it/xtx179
@r_bash