Pass bash function parameters to emacs - bash

I'm facing a problem while trying to make an emacs daemon management function in Bash.
Here is the function snippet:
function ne
{
if [ $# -ge 2 -a "$1" '==' "-s" ]
then
server="$2";
param=${#:3};
else
server="default";
param=${#:1};
fi
nbsrv=`ls ~/.emacs.d/server | grep "$server" | wc --chars`
if [ "$nbsrv" '==' "0" ]
then
echo "Starting server '$server'";
emacs --daemon=$server
fi
emacsclient --server-file=$server -nw $param;
}
It almost works, the problem is with:
param=${#:x}
For exemple, if I run:
ne -s srv1 file1 file2
It does not open me 2 new files but one named "file1 file2"
Have you got an idea of how I can make this works fine?
Thank's !
JM445
PS: Sorry if my english is not perfect, I'm french

Don't bother with bash arrays for this. Just shift off the positional parameters you don't want, and pass the remainder to emacsclient with "$#"
Your script with this modification looks like:
if [ $# -ge 2 -a "$1" '==' "-s" ]
then
server="$2";
shift 2;
else
server="default";
fi
nbsrv=`ls ~/.emacs.d/server | grep "$server" | wc --chars`
if [ "$nbsrv" '==' "0" ]
then
echo "Starting server '$server'";
emacs --daemon=$server
fi
emacsclient --server-file=$server -nw "$#";

Related

Save command output only if there's output (Unix CLI)

I have a placeholder file and would like to override it with the output of a command only if the output is not zero length. I guess I could do FOO="$(command)" then [-z $FOO]. Is there a better way?
There are different ways, but I don't know about "better". You could block on a read and only set up the redirection once some data come through:
cmd | { read j && { echo "$j"; cat; } > placeholder; }
(Note, if your command generates output but no newlines, this will ignore the data.)
If you don't need the output of the command, you can run: [ -z "$(command)" ] directly. For example, the following prints "empty":
#!/bin/sh
if [ -z "$(echo -n)" ]; then
echo "empty"
fi
Your example with -z will work, but you can also check if a variable is non-empty just with
[ "$var" ]
So, a simple solution could look like this:
#!/bin/sh
output="$( command )"
[ "$output" ] && echo "$output" > your_file.txt
If you are going to do this type of thing a lot, better make a function:
write_if_non_zero(){
local msg=$1
local file=$2
if [[ ! -z "$msg" ]]; then
echo "$msg" > "$file"
fi
}
Then
write_if_non_zero "$FOO" "$FILE"

getops still performs default actions when arguments are provided

I've recently started working with the getopts command in bash. I am confused as to why my script runs the dafult action "cat ~bin/Temp/log.txt | ~bin/Scripts/report.pl" when arguments have been provided. I only want that to run if no arguments were passed to the shell script. I've used getopts:Std in perl where I was able to code somthing like:
unless ($opts{d}) {
do something...}
How would I code something like that in a shell script? Also, how would I code logic such as this:
if ($opts{c}) {
cat ~bin/Temp/mag.txt | ~bin/Scripts/report.pl -c
}
elsif ($opts{d} {
cat ~bin/Temp/mag.txt | ~bin/Scripts/report.pl -d
My code:
#!/bin/sh
while getopts cd name
do
case $name in
c)copt=1;;
d)dopt=1;;
*)echo "Invalid arg";;
esac
done
if [[ ! -z $copt ]] #Specifies what happens if the -c argument was provided
then
echo "CSV file created!"
cat "~/bin/Temp/log.txt" | ~/bin/Scripts/vpnreport/report.pl -c
fi
if [[ ! -z $dopt ]] #Specifies what happens if the -d argument was provided
then
echo "Debug report and files created"
cat ~bin/Temp/mag.txt | ~bin/Scripts/report.pl -d
fi
if [[ ! -z $name ]] #Specifies what happens if no argument was provided
then
echo "Running standard VPN report"
cat ~bin/Temp/log.txt | ~bin/Scripts/report.pl
fi
shift $(($OPTIND -1))
My Output:
[~/bin/Scripts/report]$ sh getoptstest.sh
Running standard report
[~/bin/Scripts/report]$ sh getoptstest.sh -d
Debug report and files created
Running standard report
[~/bin/Scripts/report]$
The two getopts commands are vasty different from bash to perl and I just can't seem to get the hang of the bash varient even after reading several tutorials. Any help would be greatly appreciated!
On the final run of getopts, your variable (name) will be set to "?".
#!/bin/bash
while getopts abc foo; do :; done
echo "<$foo>"
Output of the above:
$ ./mytest.sh
<?>
$ ./mytest.sh -a
<?>
Insead, use elif, which is like Perl's elsif:
if [[ ! -z $copt ]]
then
# ...
elif [[ ! -z $dopt ]]
then
# ...
else
# ...
fi
Or test if [[ -z $copt && -z $dopt ]], or so forth. Other notes:
See the official if and case documentation in the Bash manual under "Conditional Constructs".
[[ ! -z $name ]] means the same as the more-direct [[ -n $name ]].
Use #!/bin/bash instead of #!/bin/sh, or switch off of [[ in favor of [. The double square bracket (and your use thereof) is specific to bash, and rarely works with sh.
I took Jeff's answer and rewrote my script so it works now:
#!/bin/bash
while getopts cd name
do
case $name in
c)carg=1;;
d)darg=1;;
*)echo "Invalid arg";;
esac
done
#Specifies what happens if the -c argument was provided:
if [[ ! -z $carg ]]
then
if [[ -z $darg ]]
then
echo "CSV created"
cat ~bin/Temp/log.txt | ~bin/Scripts/report.pl -c
else
echo "Debug CSV created"
cat ~bin/Temp/log.txt | ~bin/Scripts/report.pl -cd
fi
fi
#Specifies what happens if the -d argurment was provided:
if [[ ! -z $darg ]]
then
echo "Debug report created"
cat ~bin/Temp/log.txt | ~bin/Scripts/report.pl -d
#Specifies what happens if no argument was provided:
else
echo "Standard report created"
cat ~bin/Temp/logs.txt | ~bin/Scripts/report.pl
fi
Thank you again for your assistance!

Is there an rgrep alias for mac

After moving from ubuntu to mac one of the commands I've been unable to live without is rgrep. I know it's possible to use grep -r 'term' * but is there an alias (or more likely a function) that can accomplish this without the flag and *?
How about?
rgrep() {
if [ "$#" -eq 0 ]; then
echo "Usage: $FUNCNAME pattern [options] -- see grep usage"
return
# one arg - pattern
elif [ "$#" -eq 1 ]; then
grep -rn "$#" *;
# 2 args - flag pattern
elif [ "$#" -eq 2 ]; then
first=$1
shift
grep -rn "$first" "$#" *;
# more than 2 args
else
echo "Usage: $FUNCNAME 2+ params not yet supported"
return
fi
}
Put this into ~/.bashrc:
alias rgrep='grep -r'
Then start a new shell to see the alias working.

Is my syntax off or am I just doing this entirely wrong?

var1= grep "$1" ./[FILE] | wc -l
if [ -s ./[FILE] ] && [ "$var1" -eq 1 ]
then
echo "it worked"
fi
I'm trying to get var1 to have an integer value by using the command:
grep "$1" ./[FILE] | wc -l
I'm not sure if it is actually setting the value to the one that I assume the command is outputting because in my echo "$var1" it is echoing the value I expect. However, in my if statement:
if [ -s ./[FILE] ] && [ "$var1" -eq 1]
I continually get an illegal number error.
You're probably better off just doing
var1=$(grep -c "$1" ./[FILE])
-c, --count
Only a count of selected lines is written to standard output.
With regards to this:
var1= grep "$1" ./[FILE] | wc -l
if [ -s ./[FILE] ] && [ "$var1" -eq 1 ]
In shell scripting you can't have spaces on either side of the = in an assignment. The way that first line reads, you are running grep after assigning an empty string to var1. I suspect your intention was to capture the output of wc and assign that value to var1, which would be, as pointed out in another answer:
var1=$(grep "$1" ./[FILE] | wc -l)
However, as also pointed out, you can skip the wc part by changing that to
var1=$(grep -c "$1" ./[FILE])
One additional observation - if your [FILE] is zero-sized, grep isn't going to find anything, anyways, so there's really no need for that whole if ... && ... construct. Instead, those two lines could simply be:
if (( $(grep -c "$1" ./[FILE]) == 1 ))
then
echo "it worked"
....
If the echo bit is one single command, that can even be shortened to this:
(( $(grep -c "$1" ./[FILE]) == 1 )) && echo "it worked"

Bash Script check word from in txt

i need help in bash script
I have a lic.txt at domain.com which contains the string "1234567".
i want to use string "1234567". in bash script
if this word found in http://domain.com/license.txt run bash script function if not found output error license invalid how can i add this in my bash script
my bash script code is
if [ $1 ]; then
SIZE=$(($1 * 1024))
else
SIZE=$((100 * 1024))
fi
Sname=`echo $0 | sed 's/.\///g'`;
for x in $TXT_PATH/*_t.txt
do
if [ ! -e $LOCK_FILE ]; then
if [ "$x" == "$Sname" ]; then
echo -ne;
elif [ -d "$x" ] || [ -e "$x" ]; then
/bin/touch $LOCK_FILE
My command 1
My command 2
My command 3
My command 4
My command 5
My command 6
My command 7
rm -rf $LOCK_FILE
fi
else
echo "Lock file remove for run"
fi
done
If I understand the question correctly you need something like that:
wget http://domain.com/license.txt
code = $(grep 1234567 license.txt)
if [ -z $code ]; then echo "Invalid license"; else function_call_here; fi
Thanks Fredrik

Resources