1.3 KiB
1.3 KiB
Bash
File handling
- Read from file:
variable=$(<$filePath)
- Check if file (not directory or device) exists:
if [[ -f $filePath ]]
- Check if file exists:
if [[ -e $filePath ]]
- Check if directory exists:
if [[ -d $filePath ]]
- Write to file
echo "SomeString" > $filePath
- Get directory name
"$(dirname $someFile)"
-
Edit previous command
!!.s/<mistakeInCommand>/<correctedCommand>
-
Edit previous command
^<mistakeInCommand>^<correctedCommand>^
-
Check if variable is unset
-z
/ set-n
-
Iterate over files:
for f in *; do echo "File: ${f}"; done
-
Rename files somehow:
for f in *_Something_*; do echo $(echo $f | cut -d '_' -f 1,2); done
-
Replace first occurrence of pattern:
${myVar/myPattern/myString}
-
Replace all occurrences of pattern:
${myVar//myPattern/myString}
-
Remove leading
a
:temp="${opt%a}"
-
Remove trailing
a
:temp="${opt#a}"
-
Function output to variable:
result=$(myfunc)
SSH
- Execute set of commands on remote
ssh MYREMOTE /bin/bash << EOF
cmd1;
cmd2;
cmd3
EOF
if [ $? -ne 0 ]; then
# One of the commands failes
fi
-
Return code is return code of executed command
- Except if ssh fails
-
Combine command outputs:
{ command1 & command2; }
- Can also be redirected:
{ command1 & command2; } > new_file
- Can also be redirected: