Tagged Questions
8
votes
6answers
396 views
How can I test if a variable is empty or contains only spaces?
The following bash syntax verifies if param isn't empty:
[[ ! -z $param ]]
For example:
param=""
[[ ! -z $param ]] && echo "I am not zero"
No output and its fine.
But when param ...
4
votes
5answers
266 views
Delete the last character of a string using string manipulation in shell script
I would like to delete the last character of a string, I tried this little script :
#! /bin/sh
t="lkj"
t=${t:-2}
echo $t
but it prints "lkj", what I am doing wrong?
Thank you in advance!
0
votes
5answers
105 views
how to extract part of a filename before '.' or before extension
I have files in format below:
abc_asdfjhdsf_dfksfj_12345678.csv
hjjhk_hkjh_asd_asd_sd_98765498.csv
hgh_nn_25342134.exe
I want to get the value before the . and after the last _.
The result would ...
0
votes
2answers
27 views
Trim last chars from a line
I have a csv file and the lines have different length. But all of them ends with a number surrounded by a pair of braces. For example,
........{5}
........{16}
I'd like to remove the number and the ...
2
votes
6answers
1k views
How to reverse a string made of digit in bash?
What is the best way to turn out digits in number?
E.g.
$ echo 123 | hook
321
$ echo 12358 | hook
85321
2
votes
4answers
531 views
Parsing process command-line arguments from pargs in a shell script
I get list of PIDs in my bash scripts (Java processes) and have to analyze their command-line arguments to determine which instance of server each PID corresponds to.
At the moment I do it that way ...
1
vote
3answers
304 views
How to loop a read function in Bash Script?
I needed to remove spaces and capital letters to various strings of text like:
"My Name is Mauro" -> "my_name_is_mauro"
So I have created this bash script:lowercase_underscore.sh (call it the ...
1
vote
3answers
173 views
Script to split string
@terdon's answer provides this script:
find . -type f -name "*mp3" | while read file; do
album="$(basename "$(dirname "$file")")";
filename="$(basename "$file")";
...
4
votes
2answers
421 views
Add thousands separator in a number
In python
re.sub(r"(?<=.)(?=(?:...)+$)", ",", stroke )
To split a number by triplets, e.g.:
echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?<=.)(?=(?:...)+$)", ",", ...
5
votes
3answers
88 views
how do I set quotes around a variable so that the programs sees them as quote marks
I am trying to get quotes around a variable to make is just like I typed it in the terminal
to get this script to work. it shows " quotes around the varibale "
but still does not see it as quotes, as ...
1
vote
3answers
130 views
splitting the string in bash script
I am new to bash script. I am taking some input from user and checking details about that.
Example:
$HOME/Documents/test/one.txt
I take the above string as an input and want to retrieve one.txt, I ...
3
votes
5answers
181 views
Comparing Indices of two strings
I have two double quoted strings of same length, that look like this:
"$con" - (consists of spaces and *'s):
* ****** *** ** * **
and ...
0
votes
2answers
2k views
Extract a sub-string out of a variable's value
I have a string in the form of segment_78787 which is stored in a variable called $ID in my shell script. I need to get the second part "78787" and assign it to another variable.
I tried like
...
0
votes
2answers
293 views
search fitting ipv6 address for MAC-Address in neighbours
I have a list of possible to ip6 addresses converted MAC-addresses, that could be found in the output of
ping6 ff02::1%wlan0
possible mac addresses are for example:
66:70:02:75:80:fe, ...
2
votes
2answers
1k views
Testing a string containing only spaces (tabs, or “ ”)?
My code below doesn't work:
stringZ=" "
if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then
echo string is blank
else
echo string is not blank
fi
Result:
string is not blank # wrong
...
6
votes
2answers
5k 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 ...
7
votes
4answers
25k views
Splitting string by the first occurrence of a delimiter
I have a string in the next format
id;some text here with possible ; inside
and want to split it to 2 strings by first occurrence of the ;. So, it should be: id and some text here with possible ; ...
2
votes
3answers
531 views
Find word in sentence with Bourne Shell (sh)
I'm trying to determine if a particular word appears in a sentence using Bourne Shell (sh). For example:
#!/bin/bash
INPUT='Lorem ipsum dolor sit amet.'
if [[ "$INPUT" == *dolor* ]]
then
echo ...
5
votes
2answers
1k 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
...
1
vote
2answers
557 views
Checking if PATH contains $HOME/mydir and adding it if not (all in a script) [duplicate]
Possible Duplicate:
keep duplicates out of $PATH on source
I am not an expert with Unix scripting. Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from ...
7
votes
7answers
2k 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?
2
votes
2answers
9k views
Bash shell script to locate and remove substring within a filename
I am trying to write a bash shell script in Mac OS X 10.6 (which is called by an Automator Action) to rename TV show DVD rips that I have named badly over the years. I want to remove part of the text ...