리눅스 배쉬 셀, 스크립트 강의 2 - 링크드인, Kevin Dankwardt
※Title : Sourcing and aliasing with bash
* Source Scripts
- source example.sh, or . example.sh
- It is "dot space" as a short way to source a script.
- The shell executed the script in the shell's own process instead of in a new process.
- Sourceing is a common way to import variable assignments or functions.
- the sourced script is executed within the calling shell's process.
* Working with Aliases
- The alias command allows for short command alternatives: alias ll="ls -l".
- Some people use an alias to give an alias to give an alternative, more familiar name or to fix common typo to a Linux command.
- alias copy=cp
- alias rn=mv
- alias mroe=more
mroe myfile
ls -l | mroe
- List defined aliases by simply typing alias
- Unset an alias with the unalias command
명령어 실습)
cat setx.sh
chmod +x setx.sh
echo $x
./setx.sh
echo $x
source ./setx.sh
echo $x
x=1
echo $x
. ./setx.sh
echo $x
ls -l /usr/bin | more
ls -l /usr/bin | mroe
alias ls
unalias ls
=======================================
※Title : Displaying text with the echo command
*Using the Echo Command
- Built into Bash and doesn't start a new process
-n => don't print a trailing newline
-e => enable backslash escaped characters like \n and \t
-E => disable backslash escaped characters in case they were enabled by default
- ls * would list contents of directories:
echo * would show file and directory names
- Use file redirection techniques to send the output to other files, such stderr:
echo 'Warning Will Robinson!' >&2
명령어 실습 )
cat echoes.sh
./echoes.sh
=======================================
※Title : Challenges : Scripts with exported variables, sourcing, and echo
- Write a Bash script, with the "shebang" inside, that prints the variable A
- Run your script without defining or setting a variable A in your shell
- Run it again after setting A=1 on the command line before you run the script
- Finally run it after doing : export A = 2 on the command line
* Challenge: Source
- Write a script, set.sh, that sets A=10
- On the command line set A=5, and then run your script that prints A to see it print A. A should be exported.
- On the command line set A=5. In your script that prints A, before printing A, source set.sh. You should see the script now print A is 10.
- On the command line set A=5. In your script that prints A, run Set.sh; don't source it. You should see that the script prints A=5.
* Challenge: Echo
- Write a script that echoes the line
"Hello, World\n\n"
- Add the -n option to the echo and run again
- Replace the -n with -e and run again
- what do they print? How does their output differ?
=======================================
※Title : Challenge : Solutions : Scripts with exported variables, sourcing, and echo
- what do they print? How does their output differ?
정답 )
cat prA.sh
unset A
./prA.sh
A=1
./prA.sh
export A=2
./prA.sh
cat set.sh
export A
A=5
./set.sh
echo $A
cat prA2.sh
./prA2.sh
echo $A
A=5
echo $A
./set.sh
echo $A
cat prA3.sh
echo $A
./prA3.sh
=======================
cat world.sh
./world.sh
cat world2.sh
./world2.sh
cat world3.sh
./world3.sh
=======================================
※Title : Challenge : Solutions : Scripts with exported variables, sourcing, and echo