i will go and see if my touch screen calibrated with a own script.
But i have really few experience with shell scripts. I hope anyone can help me.
My idea is it to execute xinput --list-pros <device> and check the terminal output with the entry ...(242): <no items>.
This is the option if the touch screen not calibrated else there are the x/y coordinates like ...(242): 1 22 333 4444.
In my script I will execute xinput --list-pros <device> and check with grep is there a entry (242) and then check the same line if there a entry <no items>. But i fail to read the output from xinput --list.
# read the terminal output from xinput
$xinput_output= less xinput --list-pros 7
while read $xinput_output
do
# check first line from output
grep "242" $xinput_output
if [ $? != 0]
then
break;
else
# found 242 check x/y coordinates
grep "<no items>" $xinput_ouput
if [ $? != 0]
then
#no x/y coordinates, execute xinput_calibration
xinput_calibration
exit 0
fi
fi
done < $1
Enclose your command with backtick or $():
var=`some command` # note no $ before var
# Or by $()
var=$(some command)
# then you can now use command's output
echo $var
Presumably you mean xinput --list-props
Either way, you need to execute the command properly in bash, and you need to assign the variable properly, so try this:
xinput_output=$(xinput --list-props 7)
Thanks for your help,
i had a solution which works.
But i will advance it a little bit. I will remove the 'touch' command and will write the './demo' output in a memory not in a file.
Not be confused I change 'xinput' for testing in my own skript './demo', this is a script with only few 'echo' commands to generate a terminal output.
#filename: touch
#!/bin/bash
touch /tmp/tmp.log
./demo > /tmp/tmp.log
calibration=$(grep controller /tmp/tmp.log)
if [ $? != 0 ]
then
echo "missing match, corrupt file\n"
exit 0
fi
if [[ $calibration == *"<no items>"* ]]
then
echo no calibration
#xinput_calibration
else
echo found x/y coodinates
fi
rm /tmp/tmp.log
exit0
test script:
#filename: demo
#!/bin/bash
echo 'cookie'
echo 'cookie'
echo 'cookie'
controller\:\ \<no\ items\>
echo 'cookie'
echo 'cookie'
echo 'cookie'
exit 0
i ****found** the solution**. :D \"/,
My problem was, with
tmp=$(./demo)
echo $tmp
you put out the terminal output from ./demo as string.
and with 'grep' you can't find a single line.
So you must type "${tmp}" to find the single line with grep.
#cache terminal output
tmp=$(./demo)
#find word in cache
match=$(echo "${tmp}" | grep 'controller')
echo $match
Related
I am looking into how a particular exploit works, and I chose to look at one in the program 'chkrootkit' which allows for any user to run a malicious file as root. The source code for this vulnerable shellscript is as follows
slapper (){
SLAPPER_FILES="${ROOTDIR}tmp/.bugtraq ${ROOTDIR}tmp/.bugtraq.c"
SLAPPER_FILES="$SLAPPER_FILES ${ROOTDIR}tmp/.unlock ${ROOTDIR}tmp/httpd \
${ROOTDIR}tmp/update ${ROOTDIR}tmp/.cinik ${ROOTDIR}tmp/.b"a
SLAPPER_PORT="0.0:2002 |0.0:4156 |0.0:1978 |0.0:1812 |0.0:2015 "
OPT=-an
STATUS=0
file_port=
if ${netstat} "${OPT}"|${egrep} "^tcp"|${egrep} "${SLAPPER_PORT}">
/dev/null 2>&1
then
STATUS=1
[ "$SYSTEM" = "Linux" ] && file_port=`netstat -p ${OPT} | \
$egrep ^tcp|$egrep "${SLAPPER_PORT}" | ${awk} '{ print $7 }' |
tr -d :`
fi
for i in ${SLAPPER_FILES}; do
if [ -f ${i} ]; then
file_port=$file_port $i
STATUS=1
fi
done
if [ ${STATUS} -eq 1 ] ;then
echo "Warning: Possible Slapper Worm installed ($file_port)"
else
if [ "${QUIET}" != "t" ]; then echo "not infected"; fi
return ${NOT_INFECTED}
fi
}
I know that the reason the exploit works is because the line 'file_port=$file_port $i' will execute all files specified in $SLAPPER_FILES as the user chkrootkit is running (usually root), if $file_port is empty, because of missing quotation marks around the
variable assignment."
My question is why does the command
file_port=$file_port $i
result in execution of the file? Assuming that $i refers to the path of the file (/tmp/update)
I can see that file_port might be changed to some long netstat command in the previous if statement, is this something to do with it?
I've been trying to get my head around this all day to no avail, so at this point any help will be greatly appreciated :)
This is the one-shot variable assignment feature of any Bourne shell. Any command can be prefixed with zero or more variable assignments:
VAR1=VALUE1 VAR2=VALUE2 command arguments ...
Runs command arguments ... with the respective environment variables set for just this command. A typical use might be
EDITOR=vim crontab -e
I want to optimize a bash code using 1 line instead of two
this is the lines i want to optimize:
grep -E "$name" /etc/passwd
if [ $? -eq 0 ]
#...
so the if will test the exit of the last command (grep), i want to merge "grep -E "$name" /etc/passwd" in the if statement to have something like:
if [ ##### -eq 0 ]
thanks for your help :)
Without if:
grep "name" file && do_something
You can use the exit code of the command directly in the if statement:
if grep ....
then
echo "found"
else
echo "not found"
fi
If you want to silence the output of the grep command you can add the -q option, and if you want to quit after the first match (saves time on large files) you can use -m 1.
Hi I am trying to install a fairly lengthy script to install infiniband and the OFED stack on rocks cluster 6.0
here is what i try to run
user#cluster # /etc/init.d/openibd restart
/etc/init.d/openibd: line 147: syntax error near unexpected token `;&'
/etc/init.d/openibd: line 147: `if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then'
can any one share with me a fix or can identify a way to fix the error in this script?
in the file /etc/init.d/openibd
here is the part of the script which contains the error on the indicated line.
CONFIG="/etc/infiniband/openib.conf"
if [ ! -f $CONFIG ]; then
echo No InfiniBand configuration found
exit 0
fi
. $CONFIG
CWD=`pwd`
cd /etc/infiniband
WD=`pwd`
PATH=$PATH:/sbin:/usr/bin
if [ -e /etc/profile.d/ofed.sh ]; then
. /etc/profile.d/ofed.sh
fi
# Only use ONBOOT option if called by a runlevel directory.
# Therefore determine the base, follow a runlevel link name ...
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
# ... and compare them
if [ $link == $base ] ; then
RUNMODE=manual
ONBOOT=yes
else
RUNMODE=auto
fi
ACTION=$1
shift
RESTART=0
max_ports_num_in_hca=0
# Check if OpenIB configured to start automatically
if [ "X${ONBOOT}" != "Xyes" ]; then
exit 0
fi
### ERROR ON FOLLOWING LINE ###
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
if [ -n "$INIT_VERSION" ] ; then
# MODE=onboot
if LANG=C egrep -L "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ${CONFIG} > /dev/null
; then
exit 0
fi
fi
fi
You've got some HTML encoding going on their you need to fix.
Replace > with >, and replace & with &.
Your script somehow had all of its > replaced with > (and & replaced by &, etc)
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
^^
This is a syntax error because there is no command between the semi-colon that terminates the preceding command and the ampersand. The HTML encoding of certain symbols is confusing the bash parser as a result.
I wrote a shell script like this:
#! /bin/sh
...
ls | grep "android"
...
and the output is :
android1
android2
xx_android
...
I want to add a number in each file, like this:
1 android1
2 android2
3 XX_android
...
please choose your dir number:
and then wait for the user input line number x, the script reads the line number back then process the corresponding dir. How can we do this in shell ? Thanks !
nl prints line numbers:
ls | grep android | nl
If you pipe the result into cat, you can use the -n option to number each line like so:
ls | grep "android" | cat -n
Pass -n to grep, as follows:
ls | grep -n "android"
From the grep man-page:
-n, --line-number
Prefix each line of output with the line number within its input file.
Instead of implementing the interaction, you can use built-in command select.
select d in $(find . -type d -name '*android*'); do
if [ -n "$d" ]; then
# put your command here
echo "$d selected"
fi
done
The other answers on this page actually don't answer the question 100%. They don't show how to let the user interactively choose the file from another script.
The following approach will allow you to do this, as can be seen in the example. Note that the select_from_list script was pulled from this stackoverflow post
$ ls
android1 android4 android7 mac2 mac5
android2 android5 demo.sh mac3 mac6
android3 android6 mac1 mac4 mac7
$ ./demo.sh
1) android1 3) android3 5) android5 7) android7
2) android2 4) android4 6) android6 8) Quit
Please select an item: 3
Contents of file selected by user: 2.3 Android 1.5 Cupcake (API 3)
Here's the demo.sh and the script it uses to select an item from a list, select_from_list.sh
demo.sh
#!/usr/bin/env bash
# Ask the user to pick a file, and
# cat the file contents if they select a file.
OUTPUT=$(\ls | grep android | select_from_list.sh | xargs cat)
STATUS=$?
# Check if user selected something
if [ $STATUS == 0 ]
then
echo "Contents of file selected by user: $OUTPUT"
else
echo "Cancelled!"
fi
select_from_list.sh
#!/usr/bin/env bash
prompt="Please select an item:"
options=()
if [ -z "$1" ]
then
# Get options from PIPE
input=$(cat /dev/stdin)
while read -r line; do
options+=("$line")
done <<< "$input"
else
# Get options from command line
for var in "$#"
do
options+=("$var")
done
fi
# Close stdin
0<&-
# open /dev/tty as stdin
exec 0</dev/tty
PS3="$prompt "
select opt in "${options[#]}" "Quit" ; do
if (( REPLY == 1 + ${#options[#]} )) ; then
exit 1
elif (( REPLY > 0 && REPLY <= ${#options[#]} )) ; then
break
else
echo "Invalid option. Try another one."
fi
done
echo $opt
This works for me:
line-number=$(ls | grep -n "android" | cut -d: -f 1)
I use this in a script to remove sections of my sitemap.xml which I don't want Googlebot to crawl. I search for the URL (which is unique) and then find the line number using the above. Using simple maths the script then calculates the range of numbers required to delete the entire entry in the XML file.
I agree with jweyrich regarding updating your question to get better answers.
I want to run a command that gives the following output and parse it:
[VDB VIEW]
[VDB] vhctest
[BACKEND] domain.computername: ENABLED:RW:CONSISTENT
[BACKEND] domain.computername: ENABLED:RW:CONSISTENT
...
I'm only interested in some key works, such as 'ENABLED' etc. I can't search just for ENABLED as I need to parse each line at a time.
This is my first script, and I want to know if anyone can help me?
EDIT:
I now have:
cmdout=`mycommand`
while read -r line
do
#check for key words in $line
done < $cmdout
I thought this did what I wanted but it always seems to output the following right before the command output.
./myscript.sh: 29: cannot open ... : No such file
I don't want to write to a file to have to achieve this.
Here is the psudo code:
cmdout=`mycommand`
loop each line in $cmdout
if line contains $1
if line contains $2
output 1
else
output 0
The reason for the error is that
done < $cmdout
thinks that the contents of $cmdout is a filename.
You can either do:
done <<< $cmdout
or
done <<EOF
$cmdout
EOF
or
done < <(mycommand) # without using the variable at all
or
done <<< $(mycommand)
or
done <<EOF
$(mycommand)
EOF
or
mycommand | while
...
done
However, the last one creates a subshell and any variables set in the loop will be lost when the loop exits.
"How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?"
"I set variables in a loop. Why do they suddenly disappear after the loop terminates? Or, why can't I pipe data to read?"
$ cat test.sh
#!/bin/bash
while read line ; do
if [ `echo $line|grep "$1" | wc -l` != 0 ]; then
if [ `echo $line|grep "$2" | wc -l` != 0 ]; then
echo "output 1"
else
echo "output 0"
fi
fi
done
USAGE
$ cat in.txt | ./test.sh ENABLED RW
output 1
output 1
This isn't the best solution, but its a word by word translation of what you want and should give you something to start with and add your own logic