Create and edit file
This chapter shows you how to create simple files under Beckhoff RT Linux®, set their access rights, edit content with the command line editor nano and execute scripts.
Create file
- 1. Create a new file called
example.txtin your home directory with thetouchcommand:
touch ~/example.txt- 2. Check that the file has been created by listing the contents of your home directory:
ls -l ~- 3. Set the permissions for the file with
chmod. Take a look at the manpage: https://manpages.debian.org/chmod (orman chmodon your system) and select the appropriate option.
chmod [OPTION] ~/example.txt- 4. Check whether the authorizations have been set correctly by listing the contents of your home directory again:
ls -l ~Edit file
- 5. Now that you have created a file, we will edit it using the nano text editor. Nano is a command line text editor that is controlled via the keyboard.
nano ~/example.txt- 6. This will open the file
example.txtin the nano editor. Please copy the following text and paste it into the file:
#!/bin/bash
echo "My first script"
if test $# -eq 0; then
echo "No options has passed to my command"
exit 0
fi
echo "These are the passed options:"
i=1
for c in $@; do
echo "$i: $c"
i=$(($i+1))
done- 7. To save the file and exit nano, press CTRL + X, then Y to confirm the save and then ENTER to write the changes.
- 8. Check whether the file has been saved correctly by displaying its contents with the command
cat:
cat ~/example.txt- 9. You have written your first script. Execute the script with the command
bash:
bash example.txt --param1 a -f argument- 10. Instead of
bash, you can also make the script executable and run it directly. To do this, set the execution authorization withchmodor the owner of the file:
chmod u+x ~/example.txt- 11. Now you can execute the script directly:
~/example.txt --param1 a -f argument