Skip to content

Instantly share code, notes, and snippets.

@Lipen
Created October 9, 2020 10:21
Show Gist options
  • Save Lipen/c56637d8f5123d0328e38fe2d42035bc to your computer and use it in GitHub Desktop.
Save Lipen/c56637d8f5123d0328e38fe2d42035bc to your computer and use it in GitHub Desktop.
Bash Exercises - Oct 9, 2020
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bash Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "code",
"execution_count": 407,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"CURDIR=$(pwd | grep -o \"[^/]*$\")\n",
"if [ $CURDIR != \"exercises\" ]; then\n",
" mkdir -p \"exercises\"\n",
" cd \"exercises\"\n",
"fi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1. Write a shell script that prints \"Shell Scripting is Fun!\" on the screen**"
]
},
{
"cell_type": "code",
"execution_count": 290,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex1.sh\n"
]
}
],
"source": [
"%%file ex1.sh\n",
"\n",
"echo \"Shell Scripting is Fun!\""
]
},
{
"cell_type": "code",
"execution_count": 291,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shell Scripting is Fun!\n"
]
}
],
"source": [
"!bash ex1.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2. Modify the shell script from exercise 1 to include a variable. The variable will hold the contents of the message \"Shell Scripting is Fun!\"**"
]
},
{
"cell_type": "code",
"execution_count": 303,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex2.sh\n"
]
}
],
"source": [
"%%file ex2.sh\n",
"\n",
"my_msg=\"Shell Scripting is Fun!\"\n",
"echo \"${my_msg}\""
]
},
{
"cell_type": "code",
"execution_count": 304,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shell Scripting is Fun!\n"
]
}
],
"source": [
"!bash ex2.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3. Store the output of the command \"hostname\" in a variable. Display \"This script is running on _.\" where \"_\" is the output of the \"hostname\" command.**"
]
},
{
"cell_type": "code",
"execution_count": 309,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex3.sh\n"
]
}
],
"source": [
"%%file ex3.sh\n",
"\n",
"HOSTNAME=\"$(hostname)\"\n",
"\n",
"echo \"This script is running on ${HOSTNAME}\""
]
},
{
"cell_type": "code",
"execution_count": 310,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This script is running on Nighthold\n"
]
}
],
"source": [
"!bash ex3.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**4. Write a shell script to check to see if the file \"file_path\" exists. If it does exist, display \"file_path passwords are enabled.\" Next, check to see if you can write to the file. If you can, display \"You have permissions to edit \"file_path.\"\"If you cannot, display \"You do NOT have permissions to edit \"file_path\"\"**"
]
},
{
"cell_type": "code",
"execution_count": 320,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex4.sh\n"
]
}
],
"source": [
"%%file ex4.sh\n",
"\n",
"file_path=$1\n",
"\n",
"if [ -e ${file_path} ]\n",
"then\n",
" echo \"File '${file_path}' exists.\"\n",
"else\n",
" echo \"File ${file_path} DOES NOT exist.\"\n",
"fi\n",
"\n",
"if [ -w ${file_path} ]\n",
"then\n",
" echo \"File '${file_path}' is writable.\"\n",
"else\n",
" echo \"File ${file_path} IS NOT writable.\"\n",
"fi"
]
},
{
"cell_type": "code",
"execution_count": 321,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File text2 DOES NOT exist.\n",
"File text2 IS NOT writable.\n"
]
}
],
"source": [
"!bash ex4.sh text2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5. Write a shell script that displays \"man\",\"bear\",\"pig\",\"dog\",\"cat\",and \"sheep\" on the screen with each appearing on a separate line. Try to do this in as few lines as possible.**"
]
},
{
"cell_type": "code",
"execution_count": 338,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex5.sh\n"
]
}
],
"source": [
"%%file ex5.sh\n",
"animals=\"man bear pig dog cat sheep\"\n",
"for i in $animals\n",
"do\n",
" echo \"animal $i\"\n",
"done"
]
},
{
"cell_type": "code",
"execution_count": 339,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"animal man\n",
"animal bear\n",
"animal pig\n",
"animal dog\n",
"animal cat\n",
"animal sheep\n"
]
}
],
"source": [
"!bash ex5.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**6. write a shell script that prompts the user for a name of a file or directory and reports if it is a regular file, a directory, or another type of file. Also perform an ls command against the file or directory with the long listing option.**"
]
},
{
"cell_type": "code",
"execution_count": 348,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex6.sh\n"
]
}
],
"source": [
"%%file ex6.sh\n",
"\n",
"filename=$1\n",
"\n",
"if [ -d $filename ]; then\n",
" echo \"$filename is a directory\"\n",
"elif [ -e $filename ]; then\n",
" echo \"$filename is a file\"\n",
"else\n",
" echo \"$filename does not exist\"\n",
"fi"
]
},
{
"cell_type": "code",
"execution_count": 352,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"g is a directory\n"
]
}
],
"source": [
"!bash ex6.sh g"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**8. Modify the previous script to accept an unlimited number of files and directories as arguments.**"
]
},
{
"cell_type": "code",
"execution_count": 368,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex8.sh\n"
]
}
],
"source": [
"%%file ex8.sh\n",
"\n",
"args=$@\n",
"\n",
"for i in $args\n",
"do\n",
" ./ex6.sh $i\n",
"done"
]
},
{
"cell_type": "code",
"execution_count": 369,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abc does not exist\n",
"g is a directory\n",
"text is a file\n"
]
}
],
"source": [
"!bash ex8.sh abc g text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**9. Write a shell script that displays, \"This script will exit with 0 exit status.\" Be sure that the script does indeed exit with a 0 exit status.**"
]
},
{
"cell_type": "code",
"execution_count": 380,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ex9.sh\n"
]
}
],
"source": [
"%%file ex9.sh\n",
"\n",
"status=$1\n",
"\n",
"exit $status"
]
},
{
"cell_type": "code",
"execution_count": 381,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Last command exited with exit code 10\n"
]
}
],
"source": [
"%%bash\n",
"./ex9.sh 10\n",
"echo \"Last command exited with exit code $?\""
]
},
{
"cell_type": "code",
"execution_count": 403,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting cli.sh\n"
]
}
],
"source": [
"%%file cli.sh\n",
"\n",
"echo \"number of args: $#\"\n",
"\n",
"if [ $# -eq 1 ]\n",
"then\n",
" true\n",
"else\n",
" echo \"Usage: cli.sh ARG\"\n",
" exit 1\n",
"fi\n",
"\n",
"arg=$1\n",
"\n",
"# Some code for processing...\n",
"# ./process $arg\n",
"echo \"Processing with arg=${arg}...\""
]
},
{
"cell_type": "code",
"execution_count": 404,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number of args: 0\n",
"Usage: cli.sh ARG\n",
"Last command exited with exit code 1\n"
]
}
],
"source": [
"%%bash\n",
"\n",
"./cli.sh\n",
"\n",
"echo \"Last command exited with exit code $?\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**10. Write a shell script that accepts a file or directory name as an argument. Have the script report if it is reguler file, a directory, or another type of file. If it is a directory, exit with a 1 exit status. If it is some other type of file, exit with a 2 exit status.**"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex10.sh\n"
]
}
],
"source": [
"%%file ex10.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"!bash ex10.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**11. Write a script that executes the command \"cat/etc/shadow\". If the command return a 0 exit status, report \"command succeeded\" and exit with a 0 exit status. If the command returns a non-zero exit status, report \"Command failed\" and exit with a 1 exit status.**"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex11.sh\n"
]
}
],
"source": [
"%%file ex11.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"!bash ex11.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**12. Write a shell script that consists of a function that displays the number of files in the present working directory. Name this function \"file_count\" and call it in your script. If you use variable in your function, remember to make it a local variable.**"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex12.sh\n"
]
}
],
"source": [
"%%file ex12.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"!bash ex12.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**13. Modify the script from the previous exercise. Make the \"file_count\" function accept a directory as an argument. Next, have the function display the name of the directory followed by a colon. Finally display the number of files to the screen on the next line. Call the function three times. First on the \"/etc\" directory, next on the \"/var\" directory and finally on the \"/usr/bin\" directory.**"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex13.sh\n"
]
}
],
"source": [
"%%file ex13.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"!bash ex13.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**14. Write the shell script that renames all files in the current directory that end in \".jpg\" to begin with today’s date in the following format: YYYY-MM-DD. For example, if a picture of my cat was in the current directory and today was October 31,2016 it would change name from \"mycat.jpg\" to \"2016–10–31-mycat.jpg\".**"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex14.sh\n"
]
}
],
"source": [
"%%file ex14.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"!bash ex14.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**15. Write the script that renames files based on the file extension. Next,It should ask the user what prefix to prepend to the file name(s). By default, the prefix should be the current date in YYYY-MM-DD format. If the user simply press enter,the current date will be used. Otherwise,whatever the user entered will be used as the prefix. Next,it should display the original file name and new name of the file. Finally,it should rename the file.**"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex15.sh\n"
]
}
],
"source": [
"%%file ex15.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [],
"source": [
"!bash ex15.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**19. Write a shell script that exits on error and displays command as they will execute, including all expansions and substitutions. Use 3 ls command in your script. Make the first one succeed, the second one fail, and third one succeed. If you are using the proper options, the third ls command not be executed.**"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex19.sh\n"
]
}
],
"source": [
"%%file ex19.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [],
"source": [
"!bash ex19.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**20. Modify the previous exercise so that script continuous, even if an error occurs. This time, all three ls command will execute.**"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ex20.sh\n"
]
}
],
"source": [
"%%file ex20.sh\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [],
"source": [
"!bash ex20.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment