Tagged Questions
10
votes
5answers
377 views
Pattern matching on path names in bash
I want to act on a list of subdirectories in a directory. Consider:
for x in x86-headers/*/C/populate.sh; do echo $x; done
This gives
x86-headers/elf/C/populate.sh
x86-headers/gl/C/populate.sh
...
7
votes
4answers
674 views
How do I find the overlap of two strings in bash?
I have two strings. For the sake of the example they are set like this:
string1="test toast"
string2="test test"
What I want is to find the overlap starting at the beginning of the strings. With ...
7
votes
3answers
4k views
Bash - test for newline?
In various places around the web I've found:
\015
\012
\x0a - hex
\n
\r
all as synonyms for various newlines / carriage returns...
But in this small script I cannot recognise when I come across a ...
6
votes
7answers
736 views
Bash - Continuous String Manipulation
#!/bin/bash
FILE="$(basename "$1")"
FILE="${FILE/%.jpeg/.jpg}"
Is there anyway to glue these two lines together into a one-liner?
6
votes
7answers
2k views
How to check if $PWD is a subdirectory of a given path
E.g. check if $PWD is a subdirectory of /home. In other words I'm searching for a bash string operation to check if one string starts with another.
5
votes
2answers
421 views
Extracting a string, according to a pattern, in a bash script
In bash, suppose that I have a string strname:
strname="ph7go04325r"
I would like to extract the characters between the first "3" character and the last "r" character in strname, saving the result ...
5
votes
2answers
587 views
Splitting bash command line argument
Is this the best way to split up a colon separated bash command line argument?
#!/bin/bash
hostlist=`echo $1| awk '{split($0,Ip,":")} END{for (var in Ip) print Ip[var];}'`
for host in $hostlist
do
...
4
votes
2answers
52 views
Convert MAC address to Link-local address with bash
How can I convert a Mac address into an ipv6 Link-Local address?
you have to add fe80:: at the start and insert ff:fe in the middle
furthermore all leading zeros must be stripped
3
votes
2answers
162 views
filename with no spaces from two command lines
I am running two Linux commands and I want to put the resulting values together in a filename with no spaces and a dot separating the two values. So far, I've got this:
pid=`sudo dmidecode -s ...
3
votes
3answers
439 views
Extracting text from a text file in bash
I have a large text file that is all one line. In the text are several sections that looks like foo=12345 and I need to extract them all as separate lines, ignoring the rest of the file.
For ...
3
votes
3answers
2k views
How to pass a string parameter on bash function?
I have this code that does work:
get_parameter ()
{
echo "$query" | sed -n 's/^.*name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"
}
But I want to replace the "name" with the parameter that I pass ...
3
votes
1answer
901 views
bash - how to uppercase the command line argument?
I searched SO and found that to uppercase a string following would work
str="Some string"
echo ${str^^}
But I tried to do a similar thing on a command-line argument, which gave me the following ...
2
votes
4answers
5k views
how to concatenate strings in bash?
I need to concatenate 2 strings in bash, so that:
string1=hello
string2=world
mystring=string1+string2
echo mystring should produce
helloworld
2
votes
5answers
216 views
Test if a string has a period in it with bash
I want to run a bash command on output from Drupal's drush command-line interface. drush site-alias returns a list of webroots, first showing the name of the group, and then each site in that group. ...
2
votes
3answers
293 views
Collapse repeating characters
Is there a more Bashist way than echo "$PWD//" | sed -e 's#//\+#/#g' to replace repeating slashes with a single one?
Thanks Steven D, now there's a fully working function to find the longest common ...