title: "courses/bash-course - basic-statement.md"
- **fileName**: basic-statement
- **Created on**: 2024-06-07 11:05:24
echo massage
echo "welcome from yossef"
name=$(whoami)
name=$(whoami)
echo "welcome mr , $name"
man test
if [ -f /home/$USER/dotfiles]; then
echo "the file is there"
else
echo "the config file not there"
fi
man test
for statement for if checkCondition | True if | Example/explanation | |
[ -a existingfile ] | file 'existingfile' exists. | if [ -a tmp.tmp ]; thenrm -f tmp.tmp # _Make sure we're not bothered by an old temporary file_fi | |
[ -b blockspecialfile ] | file 'blockspecialfile' exists and is block special. | Block special files are special kernel files found in /dev, mainly used for ATA devices like hard disks, cd-roms and floppy disks.if [ -b /dev/fd0 ]; thendd if=floppy.img of=/dev/fd0 # _Write an image to a floppy_fi | |
[ -c characterspecialfile ] | file 'characterspecialfile' exists and is character special. | Character special files are special kernel files found in /dev, used for all kinds of purposes (audio hardware, tty's, but also /dev/null).if [ -c /dev/dsp ]; thencat raw.wav > /dev/dsp # _This actually works for certain raw wav files_fi | |
[ -d directory ] | file 'directory' exists and is a directory. | In UNIX-style, directories are a special kind of file.if [ -d ~/.kde ]; thenecho "You seem to be a kde user."fi | |
[ -e existingfile ] | file 'existingfile' exists. | (same as -a, see that entry for an example) | |
[ -f regularfile ] | file 'regularfile' exists and is a regular file. | A regular file is neither a block or character special file nor a directory.if [ -f ~/.bashrc ]; thensource ~/.bashrcfi | |
[ -g sgidfile ] | file 'sgidfile' exists and is set-group-ID. | When the SGID-bit is set on a directory, all files created in that directory will inherit the group of the directory.if [ -g . ]; thenecho "Created files are inheriting the group '$(ls -ld . | awk '{ print $4 }')' from the working directory."fi | |
[ -G fileownedbyeffectivegroup ] | file 'fileownedbyeffectivegroup' exists and is owned by the effective group ID. | The effective group id is the primary group id of the executing user.if [ ! -G file ]; then # _An exclamation mark inverts the outcome of the condition following it_chgrp $(id -g) file # _Change the group if it's not the effective one_fi | |
[ -h symboliclink ] | file 'symboliclink' exists and is a symbolic link. | if [ -h |
|
[ -k stickyfile ] | file 'stickyfile' exists and has its sticky bit set. | The sticky bit has got quite a history, but is now used to prevent world-writable directories from having their contents deletable by anyone.if [ ! -k /tmp ]; then # _An exclamation mark inverts the outcome of the condition following it_echo "Warning! Anyone can delete and/or rename your files in /tmp!"fi | |
[ -L symboliclink ] | file 'symboliclink' exists and is a symbolic link. | (same as -h, see that entry for an example) | |
[ -N modifiedsincelastread ] | file 'modifiedsincelastread' exists and was modified after the last read. | if [ -N /etc/crontab ]; thenkillall -HUP crond # _SIGHUP makes crond reread all crontabs_fi | |
[ -O fileownedbyeffectiveuser ] | file 'fileownedbyeffectiveuser' exists and is owned by the user executing the script. | if [ -O file ]; thenchmod 600 file # _Makes the file private, which is a bad idea if you don't own it_fi | |
[ -p namedpipe ] | file 'namedpipe' exists and is a named pipe. | A named pipe is a file in /dev/fd/ that can be read just once. See my bash tutorial for a case in which it's used.if [ -p $file ]; thencp $file tmp.tmp # _Make sure we'll be able to read_file="tmp.tmp" # _the file as many times as we like_fi | |
[ -r readablefile ] | file 'readablefile' exists and is readable to the script. | if [-r file ]; thencontent=$(cat file) # _Set $content to the content of the file_fi | |
[ -s nonemptyfile ] | file 'nonemptyfile' exists and has a size of more than 0 bytes. | if [ -s logfile ]; thengzip logfile # _Backup the old logfile_touch logfile # _before creating a fresh one._fi | |
[ -S socket ] | file 'socket' exists and is a socket. | A socket file is used for inter-process communication, and features an interface similar to a network connection.if [ -S /var/lib/mysql/mysql.sock ]; thenmysql --socket=/var/lib/mysql/mysql.sock # _See this MySQL tip_fi | |
[ -t openterminal ] | file descriptor 'openterminal' exists and refers to an open terminal. | Virtually everything is done using files on Linux/UNIX, and the terminal is no exception.if [ -t /dev/pts/3 ]; thenecho -e "nHello there. Message from terminal $(tty) to you." > /dev/pts/3 # _Anyone using that terminal will actually see this message!_fi | |
[ -u suidfile ] | file 'suidfile' exists and is set-user-ID. | Setting the suid-bit on a file causes execution of that file to be done with the credentials of the owner of the file, not of the executing user.if [ -u executable ]; thenecho "Running program executable as user $(ls -l executable | awk '{ print $3 }')."fi | |
[ -w writeablefile ] | file 'writeablefile' exists and is writeable to the script. | if [ -w /dev/hda ]; thengrub-install /dev/hdafi | |
[ -x executablefile ] | file 'executablefile' exists and is executable for the script. | Note that the execute permission on a directory means that it's searchable (you can see which files it contains).if [ -x /root ]; thenecho "You can view the contents of the /root directory."fi | |
[ newerfile -nt olderfile ] | file 'newerfile' was changed more recently than 'olderfile', or if 'newerfile' exists and 'olderfile' doesn't. | if [ story.txt1 -nt story.txt ]; thenecho "story.txt1 is newer than story.txt; I suggest continuing with the former."fi | |
[ olderfile -ot newerfile ] | file 'olderfile' was changed longer ago than 'newerfile', or if 'newerfile' exists and 'olderfile' doesn't. | if [ /mnt/remote/remotefile -ot localfile ]; thencp -f localfile /mnt/remote/remotefile # _Make sure the remote location has the newest version of the file, too_fi | |
[ same -ef file ] | file 'same' and file 'file' refer to the same device/inode number. | if [ /dev/cdrom -ef /dev/dvd ]; thenecho "Your primary cd drive appears to read dvd's, too."fi |
With the double-parenthesis syntax, you can use the following conditions:
Condition | True if | Example/explanation |
(( NUM1 == NUM2 )) | NUM1 is equal to NUM2. | These conditions only accept integer numbers. Strings will be converted to integer numbers, if possible. Some random examples:if (( |
(( NUM1 != NUM2 )) | NUM1 is not equal to NUM2. | |
(( NUM1 > NUM2 )) | NUM1 is greater than NUM2. | |
(( NUM1 >= NUM2 )) | NUM1 is greater than or equal to NUM2. | |
(( NUM1 < NUM2 )) | NUM1 is less than NUM2. | |
(( NUM1 <= NUM2 )) | NUM1 is less than or equal to NUM2. |
continue:exit-code.md
before: intro-bash.md