How to keep hold of another directory's path whilst using mv command?
Hi, I am getting used to bash but not there yet to be intermediate.
I would like to
Now I could manually just find the file path of the directory I would like to move this folder to, however I might not an easier way of doing this in bash. What would you suggest?
https://redd.it/xqs2bn
@r_bash
Hi, I am getting used to bash but not there yet to be intermediate.
I would like to
mv <my_folder> <destination> // #how do I find my destination file path whilst keeping this command alive? Use variables or am i missing something much simpler?Now I could manually just find the file path of the directory I would like to move this folder to, however I might not an easier way of doing this in bash. What would you suggest?
https://redd.it/xqs2bn
@r_bash
reddit
How to keep hold of another directory's path whilst using mv command?
Hi, I am getting used to bash but not there yet to be intermediate. I would like to `mv <my_folder> <destination> // #how do I find my...
One-liner Script Issue
Hello, this is my first post here. I have a question about noscripting, not specifically bash noscripting, but more general noscripting, which I hope is allowed here. I'm trying to get the api output using curl, but I need to read from a text file and pass it to the curl. Here's an example:
CURL command:
`d909afaec435463aa433899dc00111b4` is the ID of the message I want to retrieve and the result is as follows:
And I have a
Any suggestions or pointers on what's wrong with the above one-liner would be greatly appreciated. Thank you very much.
https://redd.it/xqzop5
@r_bash
Hello, this is my first post here. I have a question about noscripting, not specifically bash noscripting, but more general noscripting, which I hope is allowed here. I'm trying to get the api output using curl, but I need to read from a text file and pass it to the curl. Here's an example:
CURL command:
curl --location --request GET '`https://conversations.messages.com/v1/messages/d909afaec435463aa433899dc00111b4`' --header 'Authorization: AccessKey 123AbcDefgh456' | python3 -mjson.tool`d909afaec435463aa433899dc00111b4` is the ID of the message I want to retrieve and the result is as follows:
{"id": "d909afaec435463aa433899dc00111b4","name": "Alex Baldwin",}And I have a
msg.csv file with, say, 1,000 message IDs that I want to pass to curl. Here is my one-liner and the result.$ cat msg.csv | while read line; do curl --location --request GET '`https://conversations.messages.com/v1/messages/$line`' --header 'Authorization: AccessKey 123AbcDefgh456' | python3 -mjson.tool; done{"errors":[{"code":20,"denoscription":"Message not found"}]}Any suggestions or pointers on what's wrong with the above one-liner would be greatly appreciated. Thank you very much.
https://redd.it/xqzop5
@r_bash
how to share a variable between function calls
hi guys, hope you all doing well. i have two functions
those look like this
f1 () {
var=0
f2 &
echo $var # prints 0
}
f2 () {
var=1
sleep 10
echo $var # prints 1
}
I want the
the background (or at least have the
https://redd.it/xr212s
@r_bash
hi guys, hope you all doing well. i have two functions
f1 and f2 those look like this
f1 () {
var=0
f2 &
echo $var # prints 0
}
f2 () {
var=1
sleep 10
echo $var # prints 1
}
I want the
echo in f1 to notice the change in the $var how can i do this while having f2 run in the background (or at least have the
sleep function run in the background while f1 continues running )https://redd.it/xr212s
@r_bash
reddit
how to share a variable between function calls
hi guys, hope you all doing well. i have two functions `f1` and `f2` those look like this f1 () { var=0 f2 & echo...
Examples of cool || non-standard || "simply insane" bash stuff?
Hi guys.
So recently I encountered following project: https://github.com/Jack000/Expose
Basically, it is bash noscript which makes sort of photo-based static blog/site. Got me curious about shell noscripting in general (although I am Linux user little less than 2 decades), because most of my "shell" noscripts are actually written in Ruby.
Do you have other examples where size or purpose of Bash/shell project is unusual or non-standard? Simply something where "sane" people would use a general dynamic programming language?
Also, I found out that Chet Ramey is sole maintainer. Anyone knows what he uses to develop bash? I assume it is not Mac. :P
Thank you.
https://redd.it/xr1vce
@r_bash
Hi guys.
So recently I encountered following project: https://github.com/Jack000/Expose
Basically, it is bash noscript which makes sort of photo-based static blog/site. Got me curious about shell noscripting in general (although I am Linux user little less than 2 decades), because most of my "shell" noscripts are actually written in Ruby.
Do you have other examples where size or purpose of Bash/shell project is unusual or non-standard? Simply something where "sane" people would use a general dynamic programming language?
Also, I found out that Chet Ramey is sole maintainer. Anyone knows what he uses to develop bash? I assume it is not Mac. :P
Thank you.
https://redd.it/xr1vce
@r_bash
GitHub
GitHub - Jack000/Expose: A simple static site generator for photoessays
A simple static site generator for photoessays. Contribute to Jack000/Expose development by creating an account on GitHub.
Viewing rows based on condition with awk ?
Hi,
I'm getting disk information using
Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.7G 0 5.7G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 818M 7.0G 11% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 4.0G 2.6G 1.5G 64% /
/dev/sda2 4.0G 33M 4.0G 1% /media/sda2
/dev/sda5 509M 26M 483M 6% /media/sda5
/dev/sda6 18G 5.6G 12G 33% /media/sda6
/dev/sda3 4.0G 33M 4.0G 1% /media/sda3
tmpfs 1.6G 0 1.6G 0% /run/user/0
But I only want to see rows with storage space 30% or more used. How can I add condition to awk to get this information along with Headers
Expected Output
Filesystem Use%
/dev/sda1 64%
/dev/sda6 33%
How can I achieve this.
I have this so far but no header noscripts
df -kh | awk 'FNR == 6 {print $1" "$5}'
/dev/sda1 64%
Thank You
https://redd.it/xr1925
@r_bash
Hi,
I'm getting disk information using
df -khFilesystem Size Used Avail Use% Mounted on
devtmpfs 5.7G 0 5.7G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 818M 7.0G 11% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 4.0G 2.6G 1.5G 64% /
/dev/sda2 4.0G 33M 4.0G 1% /media/sda2
/dev/sda5 509M 26M 483M 6% /media/sda5
/dev/sda6 18G 5.6G 12G 33% /media/sda6
/dev/sda3 4.0G 33M 4.0G 1% /media/sda3
tmpfs 1.6G 0 1.6G 0% /run/user/0
But I only want to see rows with storage space 30% or more used. How can I add condition to awk to get this information along with Headers
Expected Output
Filesystem Use%
/dev/sda1 64%
/dev/sda6 33%
How can I achieve this.
I have this so far but no header noscripts
df -kh | awk 'FNR == 6 {print $1" "$5}'
/dev/sda1 64%
Thank You
https://redd.it/xr1925
@r_bash
reddit
Viewing rows based on condition with awk ?
Hi, I'm getting disk information using `df -kh` Filesystem Size Used Avail Use% Mounted on devtmpfs 5.7G 0 5.7G 0%...
Different Oout of same Script ?
Hi,
I have the following noscript:
# Text Color
RED='\0330;31m'
BLUE='\033[0;34m'
NC='\033[0m'
if [ $reqSpace -lt $availableSpace
then
echo -e "${BLUE}Backup Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
else
echo -e "${RED}Backup Not Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
fi
This noscript when run once shows as expected result
https://preview.redd.it/p5ks73exgtq91.jpg?width=697&format=pjpg&auto=webp&s=649e36d797013fdf319c49319d58f0cf1eabe974
But when run with
https://preview.redd.it/ukm9f2d2htq91.jpg?width=701&format=pjpg&auto=webp&s=c83a7ae79f05902b3507725b6f9e259290f87de8
Any idea what's going on ?
https://redd.it/xravbu
@r_bash
Hi,
I have the following noscript:
# Text Color
RED='\0330;31m'
BLUE='\033[0;34m'
NC='\033[0m'
if [ $reqSpace -lt $availableSpace
then
echo -e "${BLUE}Backup Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
else
echo -e "${RED}Backup Not Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
fi
This noscript when run once shows as expected result
https://preview.redd.it/p5ks73exgtq91.jpg?width=697&format=pjpg&auto=webp&s=649e36d797013fdf319c49319d58f0cf1eabe974
But when run with
watch command does not parse the color variables.https://preview.redd.it/ukm9f2d2htq91.jpg?width=701&format=pjpg&auto=webp&s=c83a7ae79f05902b3507725b6f9e259290f87de8
Any idea what's going on ?
https://redd.it/xravbu
@r_bash
Printing out command to terminal but not executing
Hi everyone. How do you print out a command to terminal without executing it in bash? For example, fzf's CTRL-R command will print it out to terminal before executing. How does one do that for arbitrary command?
https://redd.it/xrjtxf
@r_bash
Hi everyone. How do you print out a command to terminal without executing it in bash? For example, fzf's CTRL-R command will print it out to terminal before executing. How does one do that for arbitrary command?
https://redd.it/xrjtxf
@r_bash
reddit
Printing out command to terminal but not executing
Hi everyone. How do you print out a command to terminal without executing it in bash? For example, fzf's CTRL-R command will print it out to...
exiftools, bad substitution error when trying to modify Content Create Date metadata
I used this command last year to remove the timezone from the "Content Create Date" field in a MP4 file. But now when I run this command, I get the following error...
$ exiftool -overwrite_original "-ContentCreateDate#<${ContentCreateDate; s/[+-].*//}" "file.mp4"
bash: -ContentCreateDate#<${ContentCreateDate; s/[+-].*//}: bad substitution
Not sure why this is the case now. Is there a way to get around this? Thanks in advanced.
https://redd.it/xrpux1
@r_bash
I used this command last year to remove the timezone from the "Content Create Date" field in a MP4 file. But now when I run this command, I get the following error...
$ exiftool -overwrite_original "-ContentCreateDate#<${ContentCreateDate; s/[+-].*//}" "file.mp4"
bash: -ContentCreateDate#<${ContentCreateDate; s/[+-].*//}: bad substitution
Not sure why this is the case now. Is there a way to get around this? Thanks in advanced.
https://redd.it/xrpux1
@r_bash
reddit
exiftools, bad substitution error when trying to modify Content...
I used this command last year to remove the timezone from the "Content Create Date" field in a MP4 file. But now when I run this command, I get...
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...