שיעור 7 of 22
בתהליך

פקודות בסיסיות

מבוא (Introduction)
הפצות לינוקס מודרניות מציעות מגוון רחב של ממשקי משתמש גרפיים, אך מנהל מערכת תמיד יזדקק לדעת כיצד לעבוד עם שורת הפקודה, או כפי שהיא נקראת, ה-shell (קליפה). ה-shell הוא תוכנה המאפשרת תקשורת מבוססת טקסט בין מערכת ההפעלה למשתמש. זהו לרוב יישום במצב טקסט הקורא את קלט המשתמש ומפרש אותו כפקודות למערכת.

ישנם מספר סוגים שונים של shell בלינוקס, הנה כמה מהם:

  • Bourne-again shell (Bash)
  • C shell (csh או tcsh, הגרסה המשופרת של csh)
  • Korn shell (ksh)
  • Z shell (zsh)

בלינוקס, ה-shell הנפוץ ביותר הוא Bash. זהו גם ה-shell שישמש בדוגמאות ובתרגילים כאן.

כאשר משתמשים ב-shell אינטראקטיבי, המשתמש מכניס פקודות ב”prompt” (הנחיה). לכל הפצת לינוקס, ה-prompt ברירת המחדל עשוי להיראות קצת שונה, אך הוא בדרך כלל עוקב אחר המבנה הבא:
username@hostname current_directory shell_type

    carol@mycomputer:~$
    root@mycomputer:~#
    [dave@mycomputer ~]$
    [root@mycomputer ~]#
    

בואו נסביר כל רכיב במבנה:

  • username (שם המשתמש) – שם המשתמש שמריץ את ה-shell
  • hostname (שם המחשב המארח) – שם המחשב שעליו פועל ה-shell. יש גם פקודה בשם hostname, שדרכה ניתן להציג או להגדיר את שם המחשב.
  • current_directory (הספרייה הנוכחית) – הספרייה שבה נמצא ה-shell כרגע. הסימן ~ מציין שה-shell נמצא בספריית הבית של המשתמש הנוכחי.
  • shell_type (סוג ה-shell) – $ מציין שה-shell פועל על ידי משתמש רגיל.
  • # מציין שה-shell מופעל על ידי מנהל המערכת root.

מכיוון שאין לנו צורך בהרשאות מיוחדות, נשתמש בהנחיה ללא הרשאות מיוחדות בדוגמאות הבאות. לצורך קיצור, נשתמש רק ב-$ כהנחיה.

מבנה שורת הפקודה (Command Line Structure)
רוב הפקודות בשורת הפקודה עוקבות אחר אותו מבנה בסיסי:
command [option(s)/parameter(s)…] [argument(s)…]

    $ ls -l /home
    

בואו נסביר את מטרת כל רכיב:

Command (פקודה)
התוכנית שהמשתמש יריץ – לדוגמה, הפקודה היא ls בדוגמה למעלה.

Option(s)/Parameter(s) (אפשרות/פרמטר)
“מתג” שמשנה את התנהגות הפקודה בצורה כלשהי, כמו -l בדוגמה למעלה. אפשרויות ניתנות לגישה בצורה קצרה וארוכה. לדוגמה, -l זהה ל–format=long.
ניתן גם לשלב מספר אפשרויות ובדרך כלל ניתן להקליד את האותיות יחד בצורה הקצרה. לדוגמה, הפקודות הבאות עושות את אותו הדבר:

    $ ls -al
    $ ls -a -l
    $ ls --all --format=long
    

Argument(s) (ארגומנט)
נתונים נוספים הנדרשים על ידי התוכנית, כמו שם קובץ או נתיב, לדוגמה /home בדוגמה למעלה.

החלק היחיד שהוא חובה במבנה הזה הוא הפקודה עצמה. באופן כללי, כל שאר הרכיבים הם אופציונליים, אך ייתכן שתוכנית תדרוש אפשרויות, פרמטרים או ארגומנטים מסוימים.

הערה (NOTE)
רוב הפקודות מציגות סקירה קצרה של אפשרויות זמינות כאשר מריצים אותן עם הפרמטר –help. בקרוב נלמד דרכים נוספות ללמוד על פקודות לינוקס.

סוגי התנהגות פקודה (Command Behavior Types)
ה-shell תומך בשני סוגי פקודות:

Internal (פנימיות)
פקודות אלו הן חלק מה-shell עצמו ואינן תוכנות נפרדות. ישנן כ-30 פקודות כאלו. המטרה העיקרית שלהן היא לבצע משימות בתוך ה-shell (למשל, cd, set, export).

External (חיצוניות)
פקודות אלו נמצאות בקבצים נפרדים. קבצים אלו הם בדרך כלל תוכנות בינאריות או סקריפטים. כאשר מריצים פקודה שאינה מובנית בתוך ה-shell, ה-shell משתמש במשתנה PATH כדי לחפש קובץ הרצה עם אותו שם כמו הפקודה. בנוסף לתוכניות המותקנות עם מנהל החבילות של ההפצה, משתמשים יכולים ליצור פקודות חיצוניות משלהם.

הפקודה type מראה איזה סוג של פקודה מדובר:

    $ type echo
    echo is a shell builtin
    $ type man
    man is /usr/bin/man
    

ציטוטים (Quoting)
בתור משתמש לינוקס, תצטרכו ליצור או לשנות קבצים או משתנים בדרכים שונות. זה קל כאשר עובדים עם שמות קבצים קצרים וערכים בודדים, אך זה הופך למורכב יותר כאשר, לדוגמה, מעורבים רווחים, תווים מיוחדים ומשתנים. Shells מספקים תכונה הנקראת ציטוטים, שמאפשרת לעטוף נתונים באמצעות סוגי ציטוטים שונים (” “, ‘ ‘). ב-Bash קיימים שלושה סוגי ציטוטים:

  • ציטוטים כפולים (Double quotes)
  • ציטוטים בודדים (Single quotes)
  • תווי בריחה (Escape characters)

לדוגמה, הפקודות הבאות אינן מתנהגות באותה צורה בשל השימוש בציטוטים:

    $ TWOWORDS="two words"
    $ touch $TWOWORDS
    $ ls -l
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 two
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 words
    $ touch "$TWOWORDS"
    $ ls -l
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 two
    -rw-r--r-- 1 carol carol 0 Mar 10 14:58 'two words'
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 words
    $ touch '$TWOWORDS'
    $ ls -l
    -rw-r--r-- 1 carol carol 0 Mar 10 15:00 '$TWOWORDS'
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 two
    -rw-r--r-- 1 carol carol 0 Mar 10 14:58 'two words'
    -rw-r--r-- 1 carol carol 0 Mar 10 14:56 words
    

הערה (NOTE)
השורה עם TWOWORDS= היא משתנה Bash שיצרנו בעצמנו. נציג משתנים בהמשך. מטרת הדוגמה היא להראות כיצד ציטוטים משפיעים על הפלט של משתנים.

ציטוטים כפולים (Double Quotes)
ציטוטים כפולים אומרים ל-shell לקחת את הטקסט שבתוך סימני הציטוט (“…”) כתווים רגילים. כל התווים המיוחדים מאבדים את המשמעות שלהם, פרט לסימן הדולר ($), backslash (\) ו-backquote (`). כלומר, ניתן עדיין להשתמש במשתנים, החלפת פקודות וביצוע חישובים.

לדוגמה, ההחלפה של משתנה $USER אינה מושפעת על ידי ציטוטים כפולים:

    $ echo I am $USER
    I am tom
    $ echo "I am $USER"
    I am tom
    

שימוש בפקודת touch עם ציטוטים (touch Command with Quoting)
כפי שאתם יכולים לראות, בדוגמה הראשונה, פקודת touch יוצרת שני קבצים נפרדים, מכיוון שהפקודה מפרשת את שני המחרוזות כארגומנטים נפרדים. בדוגמה השנייה, הפקודה מפרשת את שתי המחרוזות כארגומנט אחד, ולכן היא יוצרת רק קובץ אחד. עם זאת, מומלץ להימנע משימוש ברווחים בשמות קבצים. במקום זאת, ניתן להשתמש בקו תחתון (_) או בנקודה (.)

    $ touch new file
    $ ls -l
    -rw-rw-r-- 1 tom students 0 Oct 8 15:18 file
    -rw-rw-r-- 1 tom students 0 Oct 8 15:18 new
    $ touch "new file"
    $ ls -l
    -rw-rw-r-- 1 tom students 0 Oct 8 15:19 new file
    

ציטוטים בודדים (Single Quotes)
ציטוטים בודדים אינם כוללים את החריגות של ציטוטים כפולים. הם מבטלים כל משמעות מיוחדת מכל תו. בואו ניקח אחת מהדוגמאות הראשונות:

    $ echo I am $USER
    I am tom
    

כאשר משתמשים בציטוטים בודדים, התוצאה שונה:

    $ echo 'I am $USER'
    I am $USER
    

הפקודה כעת מציגה את המחרוזת המדויקת מבלי להחליף את המשתנה.

תווי בריחה (Escape Characters)
ניתן להשתמש בתווי בריחה כדי לבטל את המשמעות המיוחדת של תווים עבור Bash. נחזור לדוגמה עם משתנה הסביבה $USER:

    $ echo $USER
    carol
    

ניתן לראות שברירת המחדל היא שהערך של המשתנה מוצג במסוף. עם זאת, אם נוסיף תו backslash (\) לפני סימן הדולר, המשמעות המיוחדת של סימן הדולר תתבטל. כתוצאה מכך, Bash לא תחליף את ערך המשתנה לשם המשתמש שמריץ את הפקודה, אלא תפרש את שם המשתנה באופן מילולי:

    $ echo \$USER
    $USER
    

אם אתם זוכרים, ניתן לקבל תוצאה דומה באמצעות ציטוטים בודדים, שמדפיסים את התוכן המילולי של מה שנמצא בין הציטוטים הבודדים. עם זאת, תו הבריחה פועל אחרת על ידי כך שהוא מורה ל-Bash להתעלם מכל משמעות מיוחדת שיש לתו שמגיע אחריו.

תרגילים מודרכים (Guided Exercises)

  1. פצלו את השורות למרכיבי פקודה, אפשרות/פרמטר וארגומנט:
    • דוגמה: cat -n /etc/passwd
      פקודה: cat
      אפשרות: -n
      ארגומנט: /etc/passwd
    • ls -l /etc
      פקודה:
      אפשרות:
      ארגומנט:
    • ls -l -a
      פקודה:
      אפשרות:
      ארגומנט:
    • cd /home/user
      פקודה:
      אפשרות:
      ארגומנט:
  2. גלה איזה סוג פקודות הן הבאות:
    • cd
    • cat
    • exit
  3. פתרו את הפקודות הבאות שמשתמשות בציטוטים:
    • echo "$HOME is my home directory"
    • touch "$USER"
    • touch 'touch'

תרגילים חקרניים (Explorational Exercises)

  1. באמצעות פקודה אחת ושימוש ב-Brace Expansion ב-Bash (עיין בדף ה-man של Bash), צור 5 קבצים ממוספרים מ-1 עד 5 עם הקידומת “game” (game1, game2, וכו’).
  2. מחק את כל 5 הקבצים שיצרת זה עתה עם פקודה אחת, תוך שימוש בתו מיוחד אחר (עיין ב-Pathname Expansion בדפי ה-man של Bash).
  3. האם קיימת דרך אחרת לגרום לשתי פקודות לפעול יחד? מהן?

סיכום (Summary)

במעבדה זו למדתם:

  • מושגי ה-Shell בלינוקס
  • מהו ה-Bash shell
  • מבנה שורת הפקודה
  • מבוא לציטוטים

פקודות שנלמדו בתרגילים:

  • bash – ה-Shell הפופולרי ביותר במחשבי לינוקס.
  • echo – מציג טקסט במסוף.
  • ls – מציג את תוכן הספרייה.
  • type – מציג כיצד מבוצעת פקודה מסוימת.
  • touch – יוצר קובץ ריק או מעדכן את תאריך השינוי של קובץ קיים.
  • hostname – מציג או משנה את שם המחשב.

תשובות לתרגילים מודרכים (Answers to Guided Exercises)

  1. פצלו את השורות למרכיבי פקודה, אפשרות/פרמטר וארגומנט:
    • ls -l /etc
      Command: ls
      Option: -l
      Argument: /etc
    • ls -l -a
      Command: ls
      Option: -l -a
      Argument:
    • cd /home/user
      Command: cd
      Option:
      Argument: /home/user
  2. גלה איזה סוג פקודות הן הבאות:
    • cd – Shell builtin
    • cat – External command
    • exit – Shell builtin
  3. פתרו את הפקודות הבאות שמשתמשות בציטוטים:
    • touch “$USER”
      יוצר קובץ בשם tom (או שם המשתמש)
    • touch ‘touch’
      יוצר קובץ בשם touch

תשובות לתרגילים חקרניים (Answers to Explorational Exercises)

  1. באמצעות פקודה אחת ושימוש ב-Brace Expansion ב-Bash (עיין בדף ה-man של Bash), צור 5 קבצים ממוספרים מ-1 עד 5 עם הקידומת “game” (game1, game2, וכו’):
    $ touch game{1..5}
    $ ls
    game1 game2 game3 game4 game5
  2. מחק את כל 5 הקבצים שיצרת זה עתה עם פקודה אחת, תוך שימוש בתו מיוחד אחר (עיין ב-Pathname Expansion בדפי ה-man של Bash):
    $ rm game?
  3. האם קיימת דרך אחרת לגרום לשתי פקודות לפעול יחד? מהן?
    כן, פקודה אחת יכולה, לדוגמה, לכתוב נתונים לקובץ אשר ינותחו על ידי פקודה אחרת. לינוקס יכולה גם לאסוף את הפלט של פקודה אחת ולהשתמש בו כקלט לפקודה אחרת. זה נקרא Piping (צינור) ונלמד על כך בשיעור עתידי.

משתנים (Variables)

משתנים הם מקומות אחסון לנתונים, כגון טקסט או מספרים. ברגע שמגדירים משתנה, ניתן לגשת לערכו מאוחר יותר. לכל משתנה יש שם שמאפשר גישה למשתנה מסוים, גם כאשר תוכן המשתנה משתנה. משתנים הם כלי נפוץ מאוד ברוב שפות התכנות.

במרבית ה-shells בלינוקס קיימים שני סוגי משתנים:

  • משתנים מקומיים (Local variables)
    משתנים אלו זמינים רק לתהליך ה-shell הנוכחי. אם תיצור משתנה מקומי ולאחר מכן תתחיל תוכנית אחרת מ-shell זה, המשתנה לא יהיה זמין לתוכנית זו יותר. מכיוון שהם אינם מועברים לתהליכים תחתיים, המשתנים הללו נקראים משתנים מקומיים.
  • משתני סביבה (Environment variables)
    משתנים אלו זמינים גם במהלך סשן shell מסוים וגם בתהליכים תחתיים שנוצרים מסשן זה. משתנים אלו יכולים לשמש להעברת נתוני תצורה לפקודות שמריצים. מכיוון שתוכניות אלו יכולות לגשת למשתנים, הם נקראים משתני סביבה. רוב משתני הסביבה כתובים באותיות גדולות (לדוגמה, PATH, DATE, USER). סט ברירת המחדל של משתני סביבה מספק, לדוגמה, מידע על ספריית הבית של המשתמש או על סוג המסוף. לפעמים כל סט משתני הסביבה נקרא “הסביבה”.

סוגי משתנים אלו ידועים גם בשם טווח המשתנים (variable scope).

הערה (NOTE)
המשתנים אינם מתמידים. כאשר סוגרים את ה-shell שבו הם הוגדרו, כל המשתנים ותוכנם נמחקים. רוב ה-shells מספקים קבצי תצורה המכילים משתנים שמוגדרים בכל פעם ש-shell חדש מופעל. משתנים שצריכים להישמר לצמיתות חייבים להיכלל באחד מקבצי תצורה אלה.

מניפולציה על משתנים (Manipulating Variables)
כמנהל מערכת, תצטרך ליצור, לשנות או להסיר משתנים מקומיים ומשתני סביבה כאחד.

עבודה עם משתנים מקומיים (Working with Local Variables)
ניתן להגדיר משתנה מקומי על ידי שימוש באופרטור = (שווה). השמה פשוטה תיצור משתנה מקומי:

$ greeting=hello

הערה (NOTE)
אל תשים רווח לפני או אחרי אופרטור =.

ניתן להציג כל משתנה באמצעות פקודת echo. הפקודה בדרך כלל מציגה את הטקסט שנמצא בחלק של הארגומנט:

$ echo greeting
greeting

כדי לגשת לערך של המשתנה תצטרך להשתמש בסימן $ (סימן דולר) לפני שם המשתנה.

המשך עבודה עם משתנים (Working with Variables)

כאשר אתה מריץ את הפקודה הבאה:

$ echo $greeting
hello

כפי שניתן לראות, המשתנה נוצר. כעת פתח shell נוסף ונסה להציג את תוכן המשתנה שנוצר:

$ echo $greeting

לא מוצג דבר. הדבר ממחיש שמשתנים קיימים רק ב-shell הספציפי בו הם נוצרו.

כדי לוודא שהמשתנה הוא אכן משתנה מקומי, נסה להפעיל תהליך חדש ובדוק אם תהליך זה יכול לגשת למשתנה. ניתן לעשות זאת על ידי התחלת shell נוסף והפעלת פקודת echo. מכיוון שה-shell החדש מופעל בתהליך חדש, הוא לא יירש משתנים מקומיים מהתהליך הראשי שלו:

$ echo $greeting world
hello world
$ bash -c ‘echo $greeting world’
world

הערה (NOTE)
הקפד להשתמש בציטוטים בודדים בדוגמה לעיל.

כדי להסיר משתנה, יש להשתמש בפקודת unset:

$ echo $greeting
hey
$ unset greeting
$ echo $greeting

הערה (NOTE)
הפקודה unset דורשת את שם המשתנה כארגומנט. לכן אין להוסיף $ לשם המשתנה, מכיוון שזה היה מפרש את ערך המשתנה במקום את שמו.

עבודה עם משתנים גלובליים (Working with Global Variables)

כדי להפוך משתנה לזמין בתהליכי משנה, יש להפוך אותו ממשתנה מקומי למשתנה סביבה (Environment Variable). הדבר נעשה באמצעות הפקודה export. כאשר היא מופעלת עם שם המשתנה, משתנה זה נוסף לסביבה של ה-shell:

$ greeting=hello
$ export greeting

הערה (NOTE)
שוב, הקפד לא להשתמש ב-$ בעת הפעלת export, מכיוון שאתה רוצה להעביר את שם המשתנה ולא את תוכנו.

דרך קלה יותר ליצור משתנה סביבה היא לשלב את שתי השיטות הנ”ל, על ידי הקצאת ערך המשתנה בחלק הארגומנט של הפקודה:

$ export greeting=hey

בוא נבדוק שוב אם המשתנה נגיש לתהליכי משנה:

$ export greeting=hey
$ echo $greeting world
hey world
$ bash -c ‘echo $greeting world’
hey world

דרך נוספת להשתמש במשתני סביבה היא להפעיל אותם לפני פקודות. נוכל לבדוק זאת עם משתנה הסביבה TZ, המכיל את אזור הזמן (Timezone). משתנה זה משמש את פקודת date כדי לקבוע את הזמן של אזור הזמן להצגה:

$ TZ=EST date
Thu 31 Jan 10:07:35 EST 2019
$ TZ=GMT date
Thu 31 Jan 15:07:35 GMT 2019

ניתן להציג את כל משתני הסביבה באמצעות הפקודה env.

משתנה PATH (The PATH Variable)

משתנה PATH הוא אחד ממשתני הסביבה החשובים ביותר במערכת לינוקס. הוא מאחסן רשימה של ספריות, המופרדות בנקודותיים, שמכילות תוכניות הרצה (executables) שיכולות לשמש כפקודות מה-shell של לינוקס.

$ echo $PATH
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

כדי להוסיף ספרייה חדשה למשתנה, יש להשתמש בסימן הנקודותיים :.

$ PATH=$PATH:new_directory

להלן דוגמה:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ PATH=$PATH:/home/user/bin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/bin

כפי שניתן לראות, $PATH משמש בערך החדש שמוקצה ל-PATH. משתנה זה מתורגם במהלך ביצוע הפקודה ומוודא שתוכן המשתנה המקורי נשמר. כמובן, ניתן להשתמש גם במשתנים אחרים בהשמה:

$ mybin=/opt/bin
$ PATH=$PATH:$mybin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/bin:/opt/bin

יש לטפל במשתנה PATH בזהירות, מכיוון שהוא קריטי לעבודה בשורת הפקודה. נבחן את משתנה PATH הבא:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

כדי לגלות כיצד ה-shell מפעיל פקודה מסוימת, ניתן להשתמש בפקודה which עם שם הפקודה כארגומנט. לדוגמה, נוכל לבדוק היכן ממוקם nano:

$ which nano
/usr/bin/nano

כפי שניתן לראות, קובץ ההפעלה של nano נמצא בתוך ספריית /usr/bin. נבצע הסרה של ספרייה זו מהמשתנה ונבדוק אם הפקודה עדיין פועלת:

$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/usr/games
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/usr/games

נחפש שוב את הפקודה nano:

$ which nano
which: no nano in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/usr/games)

כפי שניתן לראות, הפקודה לא נמצאה ולכן לא בוצעה. הודעת השגיאה גם מסבירה מדוע הפקודה לא נמצאה ובאילו מיקומים היא חופשה.

נוסיף בחזרה את הספריות וננסה להפעיל את הפקודה שוב:

$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ which nano
/usr/bin/nano

כעת הפקודה פועלת שוב.

טיפ (TIP)
סדר האלמנטים ב-PATH גם מגדיר את סדר החיפוש. הביצוע הראשון שנמצא תוך כדי מעבר דרך הנתיבים מבוצע.

תרגילים מודרכים (Guided Exercises)

  1. צור משתנה מקומי בשם number.
  2. צור משתנה סביבה (Environment Variable) בשם ORDER, תוך שימוש באחת משתי השיטות שהוזכרו לעיל.
  3. הצג את שמות המשתנים ואת התוכן שלהם.
  4. מה הם תחומי השימוש (scopes) של המשתנים שנוצרו?

תרגילים חקרניים (Explorational Exercises)

  1. צור משתנה מקומי בשם nr_files והקצה לו את מספר השורות שנמצאות בקובץ /etc/passwd.
    רמז: בדוק את הפקודה wc ואת החלפת פקודות (command substitution) ואל תשכח את סימני הציטוט.
  2. צור משתנה סביבה (Environment Variable) בשם ME. הקצה לו את הערך של המשתנה USER.
  3. הוסף את הערך של משתנה HOME ל-ME, תוך שימוש במפריד :. הצג את התוכן של המשתנה ME.
  4. בעזרת הדוגמה על הפקודה date, צור משתנה בשם today והקצה לו את התאריך עבור אחד מאזורי הזמן (time zones).
  5. צור משתנה נוסף בשם today1 והקצה לו את התאריך של המערכת.

סיכום (Summary)

במעבדה זו למדתם:

  • סוגי משתנים
  • כיצד ליצור משתנים
  • כיצד לבצע מניפולציות על משתנים

פקודות שנלמדו בתרגילים:

  • env – מציג את הסביבה הנוכחית.
  • echo – מציג טקסט.
  • export – הופך משתנים מקומיים לזמינים לתהליכי משנה.
  • unset – מסיר משתנה.

תשובות לתרגילים מודרכים (Answers to Guided Exercises)

  1. צור משתנה מקומי בשם number.
    $ number=5
  2. צור משתנה סביבה (Environment Variable) בשם ORDER, תוך שימוש באחת משתי השיטות שהוזכרו.
    $ export ORDER=desc
  3. הצג את שמות המשתנים ואת התוכן שלהם.
    $ echo number
    number
    $ echo ORDER
    ORDER
    $ echo $number
    5
    $ echo $ORDER
    desc
  4. מה הם תחומי השימוש (scopes) של המשתנים שנוצרו?
    • תחום המשתנה המקומי number הוא ה-shell הנוכחי בלבד.
    • תחום משתנה הסביבה ORDER הוא ה-shell הנוכחי וכל תתי ה-shell שנוצרים ממנו.

תשובות לתרגילים חקרניים (Answers to Explorational Exercises)

  1. צור משתנה מקומי בשם nr_files והקצה לו את מספר השורות שנמצאות בקובץ /etc/passwd.
    רמז: השתמש בפקודת wc והחלפת פקודות (command substitution) ולא לשכוח את סימני הציטוט.
    $ nr_files=`wc -l /etc/passwd`
  2. צור משתנה סביבה (Environment Variable) בשם ME והקצה לו את ערך המשתנה USER.
    $ export ME=$USER
  3. הוסף את ערך המשתנה HOME ל-ME תוך שימוש במפריד :. הצג את תוכן המשתנה ME.
    $ ME=$ME:$HOME
    $ echo $ME
    user:/home/user
  4. בעזרת הדוגמה על פקודת date, צור משתנה בשם today והקצה לו את התאריך עבור אחד מאזורי הזמן (time zones).
    הדוגמה הבאה משתמשת באזורים GMT ו-EST, אבל ניתן להשתמש בכל אזור זמן תקף.
    $ today=$(TZ=GMT date)
    $ echo $today
    Thu 31 Jan 15:07:35 GMT 2019
    או
    $ today=$(TZ=EST date)
    $ echo $today
    Thu 31 Jan 10:07:35 EST 2019
  5. צור משתנה נוסף בשם today1 והקצה לו את התאריך של המערכת.
    $ today1=$(date)
    $ echo $today1
    Thu 31 Jan 10:07:35 EST 2019

Introduction
Linux is one of the most popular operating systems. Its development was started in 1991 by Linus
Torvalds. The operating system was inspired by Unix, another operating system developed in the
1970s by AT&T Laboratories. Unix was geared towards small computers. At the time, “small”
computers were considered machines that don’t need an entire hall with air conditioning and cost
less than one million dollars. Later, they were considered as the machines that can be lifted by two
people. At that time, an affordable Unix system was not readily available on computers such as office
computers, which were tended to be based on the x86 platform. Therefore Linus, who was a student
by that time, started to implement a Unix-like operating system which was supposed to run on this
platform.
Mostly, Linux uses the same principles and basic ideas of Unix, but Linux itself doesn’t contain Unix
code, as it is an independent project. Linux is not backed by an individual company but by an
international community of programmers. As it is freely available, it can be used by anyone without
restrictions.

Distributions
A Linux distribution is a bundle that consists of a Linux kernel and a selection of applications that are
maintained by a company or user community. A distribution’s goal is to optimize the kernel and the
applications that run on the operating system for a certain use case or user group. Distributions
often include distribution-specific tools for software installation and system administration. This is
why some distributions are mainly used for desktop environments where they need to be easy to use
while others are mainly used to run on servers to use the available resources as efficient as possible.
Another way to classify distributions is by referring to the distribution family they belong to.
Distributions of the Debian distribution family use the package manager dpkg to manage the
software that is run on the operating system. Packages that can be installed with the package
manager are maintained by voluntary members of the distribution’s community. Maintainers use the
deb package format to specify how the software is installed on the operating system and how it is
configured by default. Just like a distribution, a package is a bundle of software and a corresponding
configuration and documentation that makes it easier for the user to install, update and use the
software.
The Debian GNU/Linux distribution is the biggest distribution of the Debian distribution family. The
Debian GNU/Linux Project was launched by Ian Murdock in 1993. Today thousands of volunteers
are working on the project. Debian GNU/Linux aims to provide a very reliable operating system. It
also promotes Richard Stallman’s vision of an operating system that respects the freedoms of the
user to run, study, distribute and improve the software. This is why it does not provide any
proprietary software by default.
Ubuntu is another Debian-based distribution worth mentioning. Ubuntu was created by Mark
Shuttleworth and his team in 2004, with the mission to bring an easy to use Linux desktop
environment. Ubuntu’s mission is to provide a free software to everyone across the world as well as
to cut the cost of professional services. The distribution has a scheduled release every six months
with a long-term support release every 2 years.
Red Hat is a Linux distribution developed and maintained by the identically named software
company, which was acquired by IBM in 2019. The Red Hat Linux distribution was started in 1994
and re-branded in 2003 to Red Hat Enterprise Linux, often abbreviated as RHEL. It is provided to
companies as a reliable enterprise solution that is supported by Red Hat and comes with software
that aims to ease the use of Linux in professional server environments. Some of its components
require fee-based subscriptions or licenses. The CentOS project uses the freely available source code
of Red Hat Enterprise Linux and compiles it to a distribution which is available completely free of
charge, but in return does not come with commercial support.
Both RHEL and CentOS are optimized for use in server environments. The Fedora project was
founded in 2003 and creates a Linux distribution which is aimed at desktop computers. Red Hat

initiated and maintains the Fedora distribution ever since. Fedora is very progressive and adopts new
technologies very quickly and is sometimes considered a test-bed for new technologies which later
might be included in RHEL. All Red Hat based distributions use the package format rpm.
The company SUSE was founded in 1992 in Germany as a Unix service provider. The first version of
SUSE Linux was released in 1994. Over the years SUSE Linux became mostly known for its YaST
configuration tool. This tool allows administrators to install and configure software and hardware,
set up servers and networks. Similar to RHEL, SUSE releases SUSE Linux Enterprise Server, which is
their commercial edition. This is less frequently released and is suitable for enterprise and
production deployment. It is distributed as a server as well as a desktop environment, with fit-forpurpose packages. In 2004, SUSE released the openSUSE project, which opened opportunities for
developers and users to test and further develop the system. The openSUSE distribution is freely
available to download.
Independent distributions have been released over the years. Some of them are based on either Red
Hat or Ubuntu, some are designed to improve a specific propriety of a system or hardware. There are
distributions built with specific functionalities like QubesOS, a very secure desktop environment, or
Kali Linux, which provides an environment for exploiting software vulnerabilities, mainly used by
penetration testers. Recently various super small Linux distributions are designed to specifically run
in Linux containers, such as Docker. There are also distributions built specifically for components of
embedded systems and even smart devices.
Embedded Systems
Embedded systems are a combination of computer hardware and software designed to have a
specific function within a larger system. Usually they are part of other devices and help to control
these devices. Embedded systems are found in automotive, medical and even military applications.
Due to its wide variety of applications, a variety of operating systems based on the Linux kernel was
developed in order to be used in embedded systems. A significant part of smart devices have a Linux
kernel based operating system running on it.
Therefore, with embedded systems comes embedded software. The purpose of this software is to
access the hardware and make it usable. The major advantages of Linux over any proprietary
embedded software include cross vendor platform compatibility, development, support and no
license fees. Two of the most popular embedded software projects are Android, that is mainly used
on mobile phones across a variety of vendors and Raspbian, which is used mainly on Raspberry Pi.
Android
Android is mainly a mobile operating system developed by Google. Android Inc. was founded in
2003 in Palo Alto, California. The company initially created an operating system meant to run on

digital cameras. In 2005, Google bought Android Inc. and developed it to be one of the biggest mobile
operating systems.
The base of Android is a modified version of the Linux kernel with additional open source software.
The operating system is mainly developed for touchscreen devices, but Google has developed
versions for TV and wrist watches. Different versions of Android have been developed for game
consoles, digital cameras, as well as PCs.
Android is freely available in open source as Android Open Source Project (AOSP). Google offers a
series of proprietary components in addition to the open source core of Android. These components
include applications such as Google Calendar, Google Maps, Google Mail, the Chrome browser as
well as the Google Play Store which facilitates the easy installation of apps. Most users consider
these tools an integral part of their Android experience. Therefore almost all mobile devices shipped
with Android in Europe and America include proprietary Google software.
Android on embedded devices has many advantages. The operating system is intuitive and easy to
use with a graphical user interface, it has a very wide developer community, therefore it is easy to
find help for development. It is also supported by the majority of the hardware vendors with an
Android driver, therefore it is easy and cost effective to prototype an entire system.
Raspbian and the Raspberry Pi
Raspberry Pi is a low cost, credit-card sized computer that can function as a full-functionality
desktop computer, but it can be used within an embedded Linux system. It is developed by the
Raspberry Pi Foundation, which is an educational charity based in UK. It mainly has the purpose to
teach young people to learn to program and understand the functionality of computers. The
Raspberry Pi can be designed and programmed to perform desired tasks or operations that are part
of a much more complex system.
The specialties of the Raspberry Pi include a set of General Purpose Input-Output (GPIO) pins which
can be used to attach electronic devices and extension boards. This allows using the Raspberry Pi as
a platform for hardware development. Although it was intended for educational purposes, Raspberry
Pis are used today in various DIY projects as well as for industrial prototyping when developing
embedded systems.
The Raspberry Pi uses ARM processors. Various operating systems, including Linux, run on the
Raspberry Pi. Since the Raspberry Pi does not contain a hard disk, the operating system is started
from an SD memory card. One of the most prominent Linux distributions for the Raspberry Pi is
Raspbian. As the name suggests, it belongs to the Debian distribution family. It is customized to be
installed on the Raspberry Pi hardware and provides more than 35000 packages optimized for this
environment. Besides Raspbian, numerous other Linux distributions exist for the Raspberry Pi, like,
for example, Kodi, which turns the Raspberry Pi into a media center.

Linux and the Cloud
The term cloud computing refers to a standardized way of consuming computing resources, either by
buying them from a public cloud provider or by running a private cloud. As of 2017 reports, Linux
runs 90% of the public cloud workload. Every cloud provider, from Amazon Web Services (AWS) to
Google Cloud Platform (GCP), offers different forms of Linux. Even Microsoft offers Linux-based
virtual machines in their Azure cloud today.
Linux is usually offered as part of Infrastructure as a Service (IaaS) offering. IaaS instances are virtual
machines which are provisioned within minutes in the cloud. When starting an IaaS instance, an
image is chosen which contains the data that is deployed to the new instance. Cloud providers offer
various images containing ready to run installations of both popular Linux distributions as well as
own versions of Linux. The cloud user chooses an image containing their preferred distribution and
can access a cloud instance running this distribution shortly after. Most cloud providers add tools to
their images to adjust the installation to a specific cloud instance. These tools can, for example,
extend the file systems of the image to fit the actual hard disk of the virtual machine.

Guided Exercises
1. How is Debian GNU/Linux different from Ubuntu? Name two aspects.
2. What are the most common environments/platforms Linux is used for? Name three different
environments/platforms and name one distribution you can use for each.
3. You are planning to install a Linux distribution in a new environment. Name four things that
you should consider when choosing a distribution.
4. Name three devices that the Android OS runs on, other than smartphones.
5. Explain three major advantages of cloud computing.

Explorational Exercises
1. Considering cost and performance, which distributions are mostly suitable for a business that
aims to reduce licensing costs, while keeping performance at its highest? Explain why.
2. What are the major advantages of the Raspberry Pi and which functions can they take in
business?
3. What range of distributions does Amazon Cloud Services and Google Cloud offer? Name at least
three common ones and two different ones.

Summary
In this lesson you learned:
• What distributions does Linux have
• What are Linux embedded systems
• How are Linux embedded systems used
• Different applicabilities of Android
• Different uses of a Raspberry Pi
• What is Cloud Computing
• What role does Linux play in cloud computing

Answers to Guided Exercises
1. How is Debian GNU/Linux different from Ubuntu? Name two aspects.
Ubuntu is based on a snapshot of Debian, therefore there are many similarities between them.
However, there are still significant differences between them. The first one would be the
applicability for beginners. Ubuntu is recommended for beginners because of its ease of use and
on the other hand Debian is recommended for more advanced users. The major difference is the
complexity of the user configuration that Ubuntu doesn’t require during the installation process.
Another difference would be the stability of each distribution. Debian is considered to be more
stable compared to Ubuntu. This is because Debian receives fewer updates that are tested in
detail and the entire operating system is more stable. On the other hand, Ubuntu enables the
user to use the latest releases of software and all the new technologies.
2. What are the most common environments/platforms Linux is used for? Name three different
environments/platforms and name one distribution you can use for each.
A few of the common environments/platforms would be smartphone, desktop and server. On
smartphones, it can be used by distributions such as Android. On desktop and server, it can be
used by any distribution that is mostly suitable with the functionality of that machine, from
Debian, Ubuntu to CentOS and Red Hat Enterprise Linux.
3. You are planning to install a Linux distribution in a new environment. Name four things that
you should consider when choosing a distribution.
When choosing a distribution, a few of the main things that should be considered is cost,
performance, scalability, how stable it is and the hardware demand of the system.
4. Name three devices that the Android OS runs on, other than smartphones.
Other devices that Android runs on are smart TVs, tablet computers, Android Auto and
smartwatches.
5. Explain three major advantages of cloud computing.
The major advantages of cloud computing are flexibility, easy to recover and low use cost. Cloud
based services are easy to implement and scale, depending on the business requirements. It has a
major advantage in backup and recovery solutions, as it enables businesses to recover from
incidents faster and with less repercussions. Furthermore, it reduces operation costs, as it allows
to pay just for the resources that a business uses, on a subscription-based model.
Linux Essentials (Version 1.6) | 1.1 Linux Evolution and Popular Operating Systems
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 11
Answers to Explorational Exercises
1. Considering cost and performance, which distributions are mostly suitable for a business that
aims to reduce licensing costs, while keeping performance at its highest? Explain why.
One of the most suitable distributions to be used by business is CentOS. This is because it
incorporates all Red Hat products, which are further used within their commercial operating
system, while being free to use. Similarly, Ubuntu LTS releases guarantee support for a longer
period of time. The stable versions of Debian GNU/Linux are also often used in enterprise
environments.
2. What are the major advantages of the Raspberry Pi and which functions can they take in
business?
Raspberry Pi is small in size while working as a normal computer. Furthermore, it is low cost
and can handle web traffic and many other functionalities. It can be used as a server, a firewall
and can be used as the main board for robots, and many other small devices.
3. What range of distributions does Amazon Cloud Services and Google Cloud offer? Name at least
three common ones and two different ones.
The common distributions between Amazon and Google Cloud Services are Ubuntu, CentOS and
Red Hat Enterprise Linux. Each cloud provider also offers specific distributions that the other
one doesn’t. Amazon has Amazon Linux and Kali Linux, while Google offers the use of FreeBSD
and Windows Servers.