Can someone please help with this because I can't seem to find a solution. I have the following script that works fine:
#!/bin/bash
#Checks the number of lines in the userdomains file
NUM=`awk 'END {print NR}' /etc/userdomains.hristian`;
echo $NUM
#Prints out a particular line from the file (should work with $NUM eventually)
USER=`sed -n 4p /etc/userdomains.hristian`
echo $USER
#Edits the output so that only the username is left
USER2=`echo $USER | awk '{print $NF}'`
echo $USER2
However, when I substitute the 4 on line 12 with the variable $NUM, like this, it doesn't work:
USER=`sed -n $NUMp /etc/userdomains.hristian`
I tried a number of different combinations of quotes and ${}, however none of them seem to work because I'm a BASH newbie. Help please :)
I'm not sure exactly what you've already tried but this works for me:
$ cat out
line 1
line 2
line 3
line 4
line 5
$ num=4
$ a=`sed -n ${num}p out`
$ echo "$a"
line 4
To be clear, the issue here is that you need to separate the expansion of $num from the p in the sed command. That's what the curly braces do.
Note that I'm using lowercase variable names. Uppercase ones should be be reserved for use by the shell. I would also recommend using the more modern $() syntax for command substitution:
a=$(sed -n "${num}p" out)
The double quotes around the sed command aren't necessary but they don't do any harm. In general, it's a good idea to use them around expansions.
Presumably the script in your question is a learning exercise, which is why you have done all of the steps separately. For the record, you could do the whole thing in one go like this:
awk 'END { print $NF }' /etc/userdomains.hristian
In the END block, the values from the last line in the file can still be accessed, so you can print the last field directly.
Your trying to evaluate the variable $NUMp rather than $NUM. Try this instead:
USER=`sed -n ${NUM}p /etc/userdomains.hristian`
Related
I'm diving headfirst into bash with no prior experience and have hit a bit of a snag: I wrote a small bash script to determine the average of values (it's just a total right now) being returned by a c executable.
#Sample value of #s1total: 0+0.000117+0.000149+0.000106
printf "\n%s" $s1total
#The following line works
printf "\nTotal: %s\n" $(bc <<< $s1total)
#the following also works
echo
echo -n "Total: "
echo $s1total | bc
#The following line does not work
printf "\nTotal: %s\n" $($s1total|bc)
I did eventually find that the last line can be made to work by changing it to $(echo $s1total|bc) but i don't understand why that works ...
If I run as-is, I get an error:./sievetest.sh: line 25: 0+0.000117+0.000149+0.000106: command not found
Is the string being run before the pipe? Why? Why does adding "echo" fix it?
How is the third method different than the first and second?
(as an aside, I thought the heredoc redirection operator was << , why the extra < ?)
You have this:
$($s1total|bc)
which is basically
$(0+0.000117+0.000149+0.000106 | bc)
e.g. run a command named 0+.... and pipe it to bc.
It should be
$(echo $s1total|bc)
I'm new to UNIX and have this really simple problem:
I have a text-file (input.txt) containing a string in each line. It looks like this:
House
Monkey
Car
And inside my shell script I need to read this input file line by line to get to a variable like this:
things="House,Monkey,Car"
I know this sounds easy, but I just couldnt find any simple solution for this. My closest attempt so far:
#!/bin/sh
things=""
addToString() {
things="${things},$1"
}
while read line; do addToString $line ;done <input.txt
echo $things
But this won't work. Regarding to my google research I thought the while loop would create a new sub shell, but this I was wrong there (see the comment section). Nevertheless the variable "things" was still not available in the echo later on. (I cannot just write the echo inside the while loop, because I need to work with that string later on)
Could you please help me out here? Any help will be appreciated, thank you!
What you proposed works fine! I've only made two changes here: Adding missing quotes, and handling the empty-string case.
things=""
addToString() {
if [ -n "$things" ]; then
things="${things},$1"
else
things="$1"
fi
}
while read -r line; do addToString "$line"; done <input.txt
echo "$things"
If you were piping into while read, this would create a subshell, and that would eat your variables. You aren't piping -- you're doing a <input.txt redirection. No subshell, code works without changes.
That said, there are better ways to read lists of items into shell variables. On any version of bash after 3.0:
IFS=$'\n' read -r -d '' -a things <input.txt # read into an array
printf -v things_str '%s,' "${things[#]}" # write array to a comma-separated string
echo "${things_str%,}" # print that string w/o trailing comma
...on bash 4, that first line can be:
readarray -t things <input.txt # read into an array
This is not a shell solution, but the truth is that solutions in pure shell are often excessively long and verbose. So e.g. to do string processing it is better to use special tools that are part of the “default” Unix environment.
sed ':b;N;$!bb;s/\n/,/g' < input.txt
If you want to omit empty lines, then:
sed ':b;N;$!bb;s/\n\n*/,/g' < input.txt
Speaking about your solution, it should work, but you should really always use quotes where applicable. E.g. this works for me:
things=""
while read line; do things="$things,$line"; done < input.txt
echo "$things"
(Of course, there is an issue with this code, as it outputs a leading comma. If you want to skip empty lines, just add an if check.)
This might/might not work, depending on the shell you are using. On my Ubuntu 14.04/x64, it works with both bash and dash.
To make it more reliable and independent from the shell's behavior, you can try to put the whole block into a subshell explicitly, using the (). For example:
(
things=""
addToString() {
things="${things},$1"
}
while read line; do addToString $line ;done
echo $things
) < input.txt
P.S. You can use something like this to avoid the initial comma. Without bash extensions (using short-circuit logical operators instead of the if for shortness):
test -z "$things" && things="$1" || things="${things},${1}"
Or with bash extensions:
things="${things}${things:+,}${1}"
P.P.S. How I would have done it:
tr '\n' ',' < input.txt | sed 's!,$!\n!'
You can do this too:
#!/bin/bash
while read -r i
do
[[ $things == "" ]] && things="$i" || things="$things","$i"
done < <(grep . input.txt)
echo "$things"
Output:
House,Monkey,Car
N.B:
Used grep to tackle with empty lines and the probability of not having a new line at the end of file. (Normal while read will fail to read the last line if there is no newline at the end of file.)
I am trying to check the ends of code for semi-colons as they are causing me some issues for a server I have running. To do this I am using a bash script (as I am more familiar with bash) to read through the lines and return those that doesn't end with a semi-colon. My bash script is as follows
while read line
do
if[$line!=*;]
echo $line
fi
done < $1
When I run the script, it says there is an error by fi but I cannot figure it out. I also realize this will return statements like if and while but that will be fine for my needs.
Given the sample input
use CGI;
print "<html>"
print "<head>";
print "</head>";
print "<body><p> HELLO WORLD </p>";
print "</body>";
print "</html>"
this should be the output
print "<html>"
print "</html>"
I think the easiest way would be with grep. Given an input.txt file like this:
spam
foo;<Space><Space>
sausage
baked;<Tab>
beans
unladen;
You could do
grep -v ';\s*$' input.txt
and obtain
spam
sausage
beans
grep's -v flag means "return all lines not matching this regular expression", so it will skip all lines ending with semi-colons.
If your lines have also spaces after the semi-colons, the \s* means "any sequence of space characters" so grep will remove those lines aswell.
The reason you have a problem is that your if statement requires a then. You also need some more spaces and to quote your variables. It still won't work, though. Your comparison is wrong, too - that's not how [ works to compare strings. You can use bash's [[ instead:
while read line
do
if [[ "$line" != *\; ]]
then
echo "$line"
fi
done < $1
But even with all that, what you really should be doing is:
grep -v ';$' $1
To search lines with semicolon ; and without semicolon in a linux file
$ grep ';' file_name
$ grep -v ';\s*$' file_name
I need a shell script to find and replace text that would go like:
For each line in a file
find equation mark "="
remove everything up to the equation mark on that line and replace it with the string cuts[Counter] where Counter counts how many times such substitutions have been made.
Could anybody help me out with getting started with a script like that?
Assuming you mean "up to the first equation mark..." and you want to keep the =, this should do it:
awk '{c += sub(/[^=]+=/,"cuts["c+0"]=") }1' file
In pure bash:
counts=0
while IFS= read -r line; do
if [[ "$line" =~ "=" ]]; then
echo "${counts}${line#*=}"
counts=$((counts+1))
fi
done <infile
Note that this will exclude the '='. You can reinclude it in the ehco statement if necessary.
Here is the perl one liner for that:
perl -plne 'if($.==1){$count=1}if(/=/){$_=~s/[^\=]*[=]/cut[$count]/g;$count++}' temp
I'm trying to retrieve the first 5 characters from a string and but keep getting a Bad substitution error for the string manipulation line, I have the following lines in my teststring.sh script:
TESTSTRINGONE="MOTEST"
NEWTESTSTRING=${TESTSTRINGONE:0:5}
echo ${NEWTESTSTRING}
I have went over the syntax many times and cant see what im doing wrong
Thanks
Depending on your shell, you may be able to use the following syntax:
expr substr $string $position $length
So for your example:
TESTSTRINGONE="MOTEST"
echo `expr substr ${TESTSTRINGONE} 0 5`
Alternatively,
echo 'MOTEST' | cut -c1-5
or
echo 'MOTEST' | awk '{print substr($0,0,5)}'
echo 'mystring' |cut -c1-5 is an alternative solution to ur problem.
more on unix cut program
Works here:
$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=${TESTSTRINGONE:0:5}
$ echo ${NEWTESTSTRING}
MOTES
What shell are you using?
Substrings with ${variablename:0:5} are a bash feature, not available in basic shells. Are you sure you're running this under bash? Check the shebang line (at the beginning of the script), and make sure it's #!/bin/bash, not #!/bin/sh. And make sure you don't run it with the sh command (i.e. sh scriptname), since that overrides the shebang.
This might work for you:
printf "%.5s" $TESTSTRINGONE
Works in most shells
TESTSTRINGONE="MOTEST"
NEWTESTSTRING=${TESTSTRINGONE%"${TESTSTRINGONE#?????}"}
echo ${NEWTESTSTRING}
# MOTES
echo $TESTSTRINGONE|awk '{print substr($0,0,5)}'
You were so close! Here is the easiest solution: NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})
So for your example:
$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})
$ echo $NEWTESTSTRING
MOTES
You can try sed if you like -
[jaypal:~/Temp] TESTSTRINGONE="MOTEST"
[jaypal:~/Temp] sed 's/\(.\{5\}\).*/\1/' <<< "$TESTSTRINGONE"
MOTES
That parameter expansion should work (what version of bash do you have?)
Here's another approach:
read -n 5 NEWTESTSTRING <<< "$TESTSTRINGONE"
The original syntax will work with BASH but not with DASH. On debian systems you
might think you are using bash, but maybe dash instead. If /bin/dash/exist then
try temporarily renaming dash to something like no.dash, and then create soft a
link, aka ln -s /bin/bash /bin/dash and see if that fixes the problem.
expr substr $string $position $length
$position starts from 1