我需要为我自己的脚本创建一个配置文件:
这是一个例子:
脚本:
#!/bin/bash
source /home/myuser/test/config
echo "Name=$nam" >&2
echo "Surname=$sur" >&2
Run Code Online (Sandbox Code Playgroud)
内容/home/myuser/test/config
:
nam="Mark"
sur="Brown"
Run Code Online (Sandbox Code Playgroud)
那个有效!
我的问题:这是正确的方法还是有其他方法?
我必须验证变量读取的长度(我的脚本限制为插入的五个字符),我想这样的事情:
#!/bin/bash
read string
check=${#string}
echo $check
if [ $check -ge 5 ]; then echo "error" ; exit
else echo "done"
fi
Run Code Online (Sandbox Code Playgroud)
有没有更“优雅”的解决方案?
举例来说,我有一个包含许多电子邮件地址的大文本文件,使用 bash 我需要搜索/验证电子邮件是否存在(或不存在)。应该(仅)使用“锚点”吗?
grep '^user1@example.com' text_file
Run Code Online (Sandbox Code Playgroud)
或者有更好的方法?我需要创建一个 bash 脚本,我希望安全。
我已经完成了将递归 .jpg 文件转换为其他大小的脚本:
echo $re
mkdir "$re"_tmp
for a in *.jpg ; do convert "$a" -resize $re "$re""_tmp/${a%.*} ["$re"].jpg" ; done
Run Code Online (Sandbox Code Playgroud)
我想更好地集成多扩展支持:png、bmp 等:
FILEFORMAT="jpg, JPG, png, PNG, bmp, BMP"
Run Code Online (Sandbox Code Playgroud)
有什么想法来建造它吗?
PS:变量 re 是新尺寸 1024x768(或 800x600 等)
如何查看状态apt-get update
?
$ apt-get update ; echo "status is: $?"
Err http://security.debian.org stable/updates Release.gpg
Could not resolve 'security.debian.org'
Hit http://192.168.1.100 stable Release.gpg
Hit http://192.168.1.100 stable Release
Hit http://192.168.1.100 stable/main i386 Packages
Hit http://192.168.1.100 stable/contrib i386 Packages
Hit http://192.168.1.100 stable/non-free i386 Packages
Ign http://192.168.1.100 stable/contrib Translation-en
Ign http://192.168.1.100 stable/main Translation-en
Ign http://192.168.1.100 stable/non-free Translation-en
Reading package lists... Done
W: Failed to fetch http://security.debian.org/dists/stable/updates/Release.gpg Could not resolve 'security.debian.org'
W: Some index files failed to download. They have been ignored, or …
Run Code Online (Sandbox Code Playgroud) 我可以使用该case
语句来处理参数吗?所以,使用 -restart 我想执行“-umount”和“-mount”
#!/bin/bash
case "$1" in
-mount
mount /ip/share1 /local/share1
;;
-umount
umount /ip/share1
;;
-restart
# echo TODO
;;
*)
[...]
esac
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我与以下内容的区别:
du -s dir
3705012 dir
du -s --apparent-size dir
3614558 dir
Run Code Online (Sandbox Code Playgroud)
这些目录位于块设备内(使用 cryptsetup 创建)。或者更好:为什么我只需要在加密块设备内的文件中添加 --apparent-size ?
在linux上有arp
命令:
arp -n
Address HWtype HWaddress Flags Mask Iface
192.168.1.100 ether 00:55:d1:c5:f8:1b C eth0
Run Code Online (Sandbox Code Playgroud)
和
arp-scan -I eth0 -l
Interface: eth0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.8.1 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/)
192.168.1.2 00:1b:fc:35:6a:9b ASUSTek COMPUTER INC.
192.168.1.54 c8:d3:a6:34:d2:1d (Unknown)
192.168.1.14 f0:25:b9:c2:6a:29 (Unknown)
Run Code Online (Sandbox Code Playgroud)
为什么我看不到我的设备?
我也做过:
nmap -sn 192.168.1.0/24
Run Code Online (Sandbox Code Playgroud)
使用arp
命令我看到 1 个设备,使用arp-scan
我看到其他设备。
arp
命令是否读取缓存,同时arp-scan
进行新扫描(不读取任何缓存?)
按照下面的脚本我有一个保留所有变量的唯一变量,我需要将每个变量与$VALUES
:
#!/bin/bash
shell=""
groups=""
user=""
home=""
exec 3>&1
VALUES=$(dialog --ok-label "Submit" \
--backtitle "Linux User Managment" \
--title "Useradd" \
--form "Create a new user" \
15 50 0 \
"Username:" 1 1 "$user" 1 10 10 0 \
"Shell:" 2 1 "$shell" 2 10 15 0 \
"Group:" 3 1 "$groups" 3 10 8 0 \
"HOME:" 4 1 "$home" 4 10 40 0 \
2>&1 1>&3)
exec 3>&-
echo "$VALUES"
Run Code Online (Sandbox Code Playgroud)