Pass Arguments to Command from Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass Arguments to Command from Shell Script
# 1  
Old 08-10-2015
Ubuntu Pass Arguments to Command from Shell Script

Hi all,

I am working on a project, in which I have to connect to Bluetooth low energy device. I am able to connect and do data transfer from command line. But I want to do from script
Here is my script
Code:
#!/bin/bash
#sudo hcitool -i hci0 lescan
sleep 1
sudo hcitool -i hci0 lecc --random EA:74:48:D5:52:6B
sleep 1
sudo gatttool -i hci0 -b EA:74:48:D5:52:6B --interactive
sleep 1
echo connnect //this command should be passed as arg to gatttool command
sleep 1 
char-write-cmd 0x0013 FF14
done

Here is screen shot when I run gatttool from command line
Image

Here is the screen shoot when I run from script
Image

In the above figure I have to pass command "connect" where there is red arrow from the script. I tried echo. But its not working

So please help me how to pass command
Smilie

Last edited by Franklin52; 08-10-2015 at 06:40 AM.. Reason: replaced icode tags with code tags
# 2  
Old 08-10-2015
You might want to remove the "-- interactive" option from the command in the script.

Given the tool reads from stdin, you could try piping or redirection:
Code:
echo connect | sudo gatttool ...

or (if your bash is recent enough)
Code:
sudo gatttool <<<  connect

# 3  
Old 08-10-2015
Hi,

Option --interactive is must. With out that option neither it would not connect to the device nor it will not accept gatttool as valid command.

So any other way?
# 4  
Old 08-10-2015
Did you read the entire post?
# 5  
Old 08-10-2015
Hi,

I have tried other option also.
Here is script
Code:
sudo hcitool -i hci0 lecc --random EA:74:48:D5:52:6B
sleep 1
sudo gatttool -i hci0 -b EA:74:48:D5:52:6B --interactive <<< connect

When i run above script ths is error I am getting
Code:
sh ble_connect_write.sh 
[sudo] password for embdesnithin: 
Connection handle 70
ble_connect_write.sh: 7: ble_connect_write.sh: Syntax error: redirection unexpected

# 6  
Old 08-10-2015
Quote:
Originally Posted by nithin@embdes
Here is script
Code:
sudo hcitool -i hci0 lecc --random EA:74:48:D5:52:6B
sleep 1
sudo gatttool -i hci0 -b EA:74:48:D5:52:6B --interactive <<< connect

I think the biggest problem is here is the misconception about what sudo does and what it doesn't:

sudo will run a command as another user (usually as root), but: it is NOT a shell and neither it is a shell replacement!

I wonder why the user account you use to run the script is not allowed to run the commands within the script. If the commands are privileged in nature then use a privileged user which can run them natively. call the whole script via sudo instead of embedding sudo-enabled calls into the script.

If you really need to do it the way you tried use su instead of sudo and use sudo only to call the script:

Code:
#! /bin/sh
[...]
su - <user> -c <your command along with options>
[...]

Code:
user@sys # sudo <scriptname>

I hope this helps.

bakunin
# 7  
Old 08-10-2015
I am using bash instead of sh.

I tried "su". But of no use.
script:
Code:
su - embdesnithin --command hcitool -i hci0 lecc --random EA:74:48:D5:52:6B
sleep 1
su - embdesnithin --command gatttool -i hci0 -b EA:74:48:D5:52:6B -I <<< connect
sleep 1

Error:

Code:
embdesnithin@embdesnithin:~$ sudo sh ble_connect_write.sh 
su: invalid option -- 'i'
Usage: su [options] [LOGIN]

Options:
  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

ble_connect_write.sh: 7: ble_connect_write.sh: Syntax error: redirection unexpected

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to pass arguments while running a Shell Script

Shell Script Gurus, I am writing a shell script which needs User ID's to pass as an Arguments in command line while executing. Can this be doable? I've never done this, If you give a sample script that would be helpful, Thanks. (1 Reply)
Discussion started by: shekar777
1 Replies

2. Shell Programming and Scripting

How to pass arguments from a textbox in CGI to a shell script?

Hello All, Could you please help me to explain that how do I pass textboxes values while running a shell script which need some arguments while running it and should be invoked after the button click. Here is an example. The first CGI is: #!/bin/ksh echo "Content-Type: text/html"... (4 Replies)
Discussion started by: RavinderSingh13
4 Replies

3. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

How can I pass arguments to system command in a awk script?

Hi I need your help, please How can I pass arguments to system command in a awk script?... for example: byte=substr(cadena,pos,2); system("grep -n byte mapeo.txt"); Does it exist a way? Thanks for advance. (4 Replies)
Discussion started by: solaris21
4 Replies

6. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

7. Shell Programming and Scripting

[C SHELL] How to pass dynamic number of arguments

I have a task. The scenario is like this. I have a operation program (OPR1) , whose function is to simply double the (single)value it receives as input. I have to write a script to operate the OPR1 and save its output in a file. Later, I have to modify the script so as to be able to operate ... (0 Replies)
Discussion started by: animesharma
0 Replies

8. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

9. Shell Programming and Scripting

Need help to pass arguments to shell script

Hi, I have a shell script called ftp.sh which is running continously in background. I tried passing arguments to this script but it did not worked out. Below is ftp.sh script. Please help me case $param in start) sleep_func "300" echo "!ksh $scr_ddir/ftp.sh... (1 Reply)
Discussion started by: bhargav20
1 Replies

10. Shell Programming and Scripting

How to pass arguments to a function in a shell script?

Hi, I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments. The function written is f1() {...... ........ } What should... (3 Replies)
Discussion started by: preetikate
3 Replies
Login or Register to Ask a Question