Writing to the serial port from the Linux command line
It gives the output on the standard output. more : It is a filter for paging through text one screenful at a time. less : It is used to viewing the files instead of opening the file. File Permissions Commands: The chmod and chown commands are used to control access to files in UNIX and Linux systems.
What is Shell in Linux?
Introduction to Shell in Linux
The Linux is a code that transmits the system commands., Compilers, Editors, linkers and command-line interpreters are important and useful but they are not part of the operating system. We will look briefly at the LINUX command interpreter , called the SHELL, which, although not part of the operating system, makes heavy use of many operating system features and thus serves as a good example of how the system calls can be used. It is also the primary interface between a user sitting at his terminal and the operating system.
Shell has standard input and output as its terminal. Shell is started when a user begins to login. To start a command a dollar sign is typed which indicates the user that the shell is ready to accept the command. For instance, when a user gives the date as input the there is a child process created and a program begins to run as a child process. Only when the child process is terminated after execution the shell gets back to the prompt and types again and after that, it reads the following lines as input.
Examples
Following are the different examples:
It prints the current date and time.
Command: $date
The user can specify that the standard output be redirected to a file,
Command: $date > file
The user can specify that standard input can be redirected, as in
Command: $sort file2
Which invokes the sort program with input takes from file1 and output sent to file2
The pipe helps in connecting the output of a particular program as input to other programs.
Command: $cat file1 file2 file3 | sort>/dev/lp
Which invokes the cat program to concatenate three files and send the output to sort to arrange all the lines in alphabetical order. The output of sort is redirected to the file /dev/lp, which is a typical name for the special character file for the printer.
Types of Shell
SHELL determines the type of shell that a user sees logging in. Today, there is a host of the shell that accompanies any LINUX system, and you can select the one you like the most. Besides the Bourne shell which is the most popular in the LINUX world, the C and Korn shells have also carved out a niche for themselves, because of certain inherent advantages. The C shell is known by the program csh, the Korn shell by ksh and the bourne shell by sh.
If you wish to use any of the above shell types as the default shell, then the variable needs to be assigned accordingly. However, this assignment is made by the system after reading a field in the file /etc./passwd. If you wish to change the setting permanently, then this file needs to be edited. The system administrator usually sets up your login shell while creating a user account, though you can change it whenever you request to.
Shell Keywords
Keywords are the words, are called reserved words. Following is the list of keywords available in the Bourne shell.
Echo | if | until | trap |
read | else | case | wait |
set | fi | esac | eval |
unset | while | break | exec |
read-only | do | continue | ulmit |
shift | Done | exit | umask |
export | For | return |
1. Unchanging variables- set keyword
In some applications, a need may arise for variables to have a constant or fixed value. For instants, if we want that a’s variable should always remain 20 and not changed, we can achieve this by saying,
The shell will not permit to change a value when they are created as read-only. Read-only variables can be created by typing read-only at a command prompt.
When there is a need to clear or erase a particular command from the shell, we will use the “unset” keyword as a command.
$a = 20
$echo a
20
$unset a
$echo a // a have no value now so it prints nothing
2. Echo keyword
To print either the value of any variable or words under double quotation.
3. Cd command: read keyword
The read statement is the shell’s internal tool for taking input from the standard input. Functionally, it is similar to the INPUT statement of BASIC and the scanf() function in C. But it has one or two interesting features, it can be used with one or more variables to make shell scripts interactive. The input supplied through the standard input during interactive is read into these variables. The script emp1.sh uses the statement to take the search string and the filenames from the terminal.
Command – Shell in Linux
$cat emp1.sh
#Script : emp1.sh — Interactive version
#The pattern and filename to be supplied by the user
echo «\nEnter the pattern to be searched : \c»
read pname
echo «\nEnter the file to be used :\c»
read flname
echo «\nSearching for $pname from the $flname\n»
grep «$pname» $flname
echo «\nSelected records shown above»
$_
Run it, and specify the input accordingly
$emp1.sh
Enter the pattern to be searched: director
Enter the file to be used: emp2.lst
Searching for director from file emp2.lst
Read can also be used to make the script pause without accepting any input from the user. It can simply wait for the key to be pressed for program execution to continue (somewhat like the WAIT command of dBASE). You can also condense the above program to some extent by accepting both the arguments in one line; a single read can also be used with two or more variables names.
Conclusion
In a Linux/Unix operating system, the function of the shell is to interpret the command output. It acts as an interface to run the commands. Linux and Unix shell have additional features than a command Interpreter , it acts as a programming language with basic structures like conditional loops, functions, variables, etc. Shell is a powerful environment that can be used to run the commands and works also as a programming language by using the input commands and executing them. Hence shell is widely used Interface due to its features, a Linux/Unix Shell has more advantages than a Windows Shell.
Final thoughts
This is a guide to What is Shell in Linux? Here we discuss the introduction, types of Shell along with Commands and respective examples. You can also go through our other related articles to learn more–
What is Shell in Linux? | Types of Shell with Commands, Following are the different examples: Example #1 It prints the current date and time. Command: Example #2 The user can specify that the standard output be redirected to a file, Command: Example #3 The user can specify that standar…
Writing to the serial port from the Linux command line
From Windows I can communicate with a serial port device using the following commands:
mode com1: baud=9600 data=8 parity=n stop=1 copy con com1 alt+18alt+2ctrl+z
The device starts the requested operation.
When I try to accomplish the same operation from a stand-alone Debian box or from a Debian VirtualBox instance of the same Windows machine, I had no luck so far.
Here are equivalent Linux commands (at least I think so):
stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb echo '\x12\x02' > /dev/ttyS0
If you want to use hexadecimal codes, you should add the -e option to enable interpretation of backslash escapes by echo (but the result is the same as with echo Ctrl + R , Ctrl + B ). And as wallyk said, you probably want to add -n to prevent the output of a newline:
Also make sure that /dev/ttyS0 is the port you want.
will not be interpreted, and will literally write the string \x12\x02 (and append a newline) to the specified serial port. Instead use
which you can construct on the command line by typing Ctrl V Ctrl R and Ctrl V Ctrl B . Or it is easier to use an editor to type into a script file.
The stty command should work, unless another program is interfering. A common culprit is gpsd which looks for GPS devices being plugged in.
Using Screen:
Note : Screen is actually not able to send hexadecimal, as far as I know. To do that, use echo or printf .
I was using the suggestions in this post to write to a serial port, and then using the information from another post to read from the port, with mixed results. I found that using Screen is an «easier» solution, since it opens a terminal session directly with that port. (I put easier in quotes, because Screen has a really weird interface, IMO, and takes some further reading to figure it out.)
You can issue this command to open a screen session, and then anything you type will be sent to the port, plus the return values will be printed below it:
(Change the above to fit your needs for speed, parity, stop bits, etc.) I realize Screen isn’t the «Linux command line» as the post specifically asks for, but I think it’s in the same spirit. Plus, you don’t have to type echo and quotes every time.
echo
It follows praetorian droid’s answer. However , this didn’t work for me until I also used the cat command ( cat < /dev/ttyS0 ) while I was sending the echo command.
printf
I found that one can also use printf’s ‘%x’ command:
c="\x"$(printf '%x' 0x12) printf $c >> $SERIAL_COMM_PORT
Writing to the serial port from the Linux command line, Add a comment. 17. echo ‘\x12\x02’. will not be interpreted, and will literally write the string \x12\x02 (and append a newline) to the specified serial port. Instead use. echo -n ^R^B. which you can construct on the command line by typing Ctrl V Ctrl R and Ctrl V Ctrl B. Or it is easier to use an editor to type …
Basic Shell Commands in Linux
A shell is a special user program that provides an interface to the user to use operating system services. Shell accepts human-readable commands from the user and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
1). Displaying the file contents on the terminal:
- cat : It is generally used to concatenate the files. It gives the output on the standard output.
- more : It is a filter for paging through text one screenful at a time.
- less : It is used to viewing the files instead of opening the file.Similar to more command but it allows backward as well as forward movement.
- head : Used to print the first N lines of a file. It accepts N as input and the default value of N is 10.
- tail : Used to print the last N-1 lines of a file. It accepts N as input and the default value of N is 10.
2). File and Directory Manipulation Commands:
- mkdir : Used to create a directory if not already exist. It accepts the directory name as an input parameter.
- cp : This command will copy the files and directories from the source path to the destination path. It can copy a file/directory with the new name to the destination path. It accepts the source file/directory and destination file/directory.
- mv : Used to move the files or directories. This command’s working is almost similar to cp command but it deletes a copy of the file or directory from the source path.
3). Extract, sort , and filter data Commands:
- grep with Regular Expressions : Used to search for text using specific regular expressions in file.
4). Basic Terminal Navigation Commands:
- ls : To get the list of all the files or folders.
- ls -l: Optional flags are added to ls to modify default behavior, listing contents in extended form -l is used for “long” output
- ls -a: Lists of all files including the hidden files, add -a flag
- cd : Used to change the directory.
- du : Show disk usage.
- pwd : Show the present working directory.
- man : Used to show the manual of any command present in Linux.
- rmdir : It is used to delete a directory if it is empty.
- lnfile1 file2 : Creates a physical link.
- ln-s file1 file2 : Creates a symbolic link.
- locate: It is used to locate a file in Linux System
- echo: This command helps us move some data, usually text into a file.
- df: It is used to see the available disk space in each of the partitions in your system.
- tar: Used to work with tarballs (or files compressed in a tarball archive)
5). File Permissions Commands: The chmod and chown commands are used to control access to files in UNIX and Linux systems.
- chown : Used to change the owner of the file.
- chgrp : Used to change the group owner of the file.
- chmod : Used to modify the access/permission of a user.
Linux list ports command Code Example, # Any of the following sudo lsof -i -P -n | grep LISTEN sudo netstat -tulpn | grep LISTEN sudo lsof -i:22 # see a specific port such as 22 sudo nmap -sTU …