Welcome to the Linux Foundation Forum!
C take input from Bash Script
usmangt
Posts: 42
Hi,
I make a simple c file which takes input from user and then display it. I am trying to make a bash script in which i predefined those input and then run the c program inside bash and the c program takes the input which i have defined (i.e. user don't need to enter the input)
Here is the code.
#include<stdio.h>
void main()
{
char ch;
int num;
printf("Enter a single character:\n");
scanf("%c", &ch);
printf("Enter any number:\n");
scanf("%d", &num);
printf("You type charachter %c\n", ch);
printf("You type number %d\n", num);
}
void main()
{
char ch;
int num;
printf("Enter a single character:\n");
scanf("%c", &ch);
printf("Enter any number:\n");
scanf("%d", &num);
printf("You type charachter %c\n", ch);
printf("You type number %d\n", num);
}
And here is the bash file
#!/bin/bash
ch=s
num=2
./test < $ch $num
ch=s
num=2
./test < $ch $num
When i run the bash script it don't take the input values of ch and num. then i find on Google to use | so i tried ./test | $ch $num but didn't work
Please help me.
Thank you
0
Comments
-
./test << EOF
$ch
$num
EOF0 -
What you did in your bash script is correct, but you might as well write "./test s 2"
In fact, when bash encounter '$varname', it replace it first with the value of the variable varname, before the function call, so the line is converted to "./test s 2" before bash calls the c file, which is the moment when ./test s 2 is converted to an array with tree pointer to "./test\0", "s\0" and "2\0".
This array can be acceded by defining main like "int main (int argc, char ** argv)"
(argc and argv are named by convention, but there types ca not be changed)
argv is the array bash where bash putted the words of the line that called the c file.
You can then access to 's' and '2' with argv[1][0] and argv[2][0].
The next thing you might want to do is convert 2 in an integer, so you might find this (http://stackoverflow.com...convert-string-to-integer...) usefull.0 -
usmangt wrote:And here is the bash file
#!/bin/bash ch=s num=2 ./test < $ch $num
When i run the bash script it don't take the input values of ch and num. then i find on Google to use | so i tried ./test | $ch $num but didn't work
Please help me.
Thank you
You are treating $ch like it is a file and you are using the pipe symbol backwards.
Try:echo $ch $num | ./test
0
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- 1 Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 1.4K LFS258 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 946 Programming and Development
- 310 Kernel Development
- 618 Software Development
- 981 Software
- 373 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)