script-detector.sh
#!/bin/bash
#Description:check all Shells in given one directory.
#测试前两个字符
testchars=2
#默认脚本都是以"#!"开头的
shabang='#!'
#遍历当前目录(只针对当前目录)下的所有文件
for file in *
do
if [[ $(head -c$testchars "$file") = "$shabang" ]];then
#-c:从文件开头输出制定个数的字符,而不是默认的行数
echo "File \"$file\" is a script."
else
echo "File \"$file\" is not a script."
fi
done
exit 0
验证:
[root@logstash ~]# sh script-detector.sh
File "1.txt" is not a script.
File "22.sh" is a script.
File "2.txt" is not a script.
File "access_20240624.log" is not a script.
File "account.sh" is a script.
File "add_description.sh" is a script.
File "add_firewalld_rule.sh" is a script.
File "anaconda-ks.cfg" is not a script.
File "auto_backup_system.sh" is a script.
File "awk.sh" is a script.
File "back_mysql.sh" is a script.
File "backup.sh" is a script.
File "call_names.sh" is a script.
File "check_rpm.sh" is a script.
File "chess.sh" is a script.
File "clear_record_file.sh" is a script.
File "close_machines.sh" is a script.
File "create_lv.sh" is a script.
File "crontab.sh" is a script.
File "date.sh" is a script.
File "delete_file.sh" is a script.
File "digital_file.sh" is a script.
File "disable_selinux.sh" is a script.
File "display_account.sh" is a script.
File "display_cpu.sh" is a script.
File "display_port.sh" is a script.
File "domains.txt" is not a script.
File "double-colored-ball.sh" is a script.
File "double-colored-ball.txt" is not a script.
File "download_pic.sh" is a script.
File "egrep.sh" is a script.
File "ens32_packet.sh" is a script.
File "expect_command.sh" is a script.
File "fdisk_disk.sh" is a script.
File "fibonacci_sequence.sh" is a script.
File "file_comparison.sh" is a script.
File "file_directory.sh" is a script.
File "file_limits.sh" is a script.
File "file.sh" is a script.
File "ftp.sh" is a script.
File "generate_certificate.sh" is a script.
File "generate_random_code.sh" is a script.
File "generate_string_pass.sh" is a script.
File "generate_urandom.sh" is a script.
File "generate_uuid.sh" is a script.
File "graph.sh" is a script.
File "guess_number.sh" is a script.
File "helloworld.sh" is a script.
File "hive.output" is a script.
File "hive-server2.log" is not a script.
File "hive.sh" is a script.
File "host.info" is not a script.
File "hostname-bak.txt" is not a script.
File "hostname.sh" is a script.
File "hostname.txt" is not a script.
File "inotifywait.sh" is a script.
File "Jan-Ken-Punch.sh" is a script.
File "jdk-11.0.10_linux-x64_bin.tar.gz" is not a script.
File "judge_data_type.sh" is a script.
File "judge_leap_year.sh" is a script.
File "judge_user_level.sh" is a script.
File "judge_user_passwd.sh" is a script.
File "judge_yes_no.sh" is a script.
File "lamp.sh" is a script.
File "logback.sh" is a script.
File "log_count.sh" is a script.
File "logstash-7.11.1-linux-x86_64.tar.gz" is not a script.
File "lowercase_uppercase.sh" is a script.
File "mac.sh" is a script.
File "md5_check.sh" is a script.
File "monitor_http.sh" is a script.
File "multiplication_table.sh" is a script.
File "mysql_count.sh" is a script.
File "mysql_ping.sh" is a script.
File "network_statics.sh" is a script.
File "nginx_ip.sh" is a script.
File "nginx_time_ip.sh" is a script.
File "nginx_time_statics_ip.sh" is a script.
File "number_length.sh" is a script.
File "optimize_kernel.sh" is a script.
File "parameter.sh" is a script.
File "process_bar.sh" is a script.
File "process.sh" is a script.
File "read.sh" is a script.
File "root_count.sh" is a script.
File "rsyncd.sh" is a script.
File "schedule-bak.sh" is a script.
File "script-detector.sh" is a script.
File "self-destruct.sh" is a script.
File "server_source_cpu.sh" is a script.
File "server_source_disk.sh" is a script.
File "server_source_memory.sh" is a script.
File "server_source_tcp_status.sh" is a script.
File "split_nginx_log.sh" is a script.
File "ssh_key.sh" is a script.
File "statistic.sh" is a script.
File "sum.sh" is a script.
head: error reading ‘test’: Is a directory
File "test" is not a script.
File "text.sh" is a script.
File "time.sh" is a script.
File "tmp.sh" is a script.
File "tomcat_pid.sh" is a script.
File "tomcat_restart.sh" is a script.
File "tomcat.sh" is a script.
File "t.sh" is a script.
File "user_account.sh" is a script.
File "user_add.sh" is a script.
File "useradd.sh" is a script.
File "user_del.sh" is a script.
File "user_get_passwd.sh" is a script.
File "user_list.txt" is not a script.
File "user.txt" is not a script.
File "website_check.sh" is a script.
File "zombie.sh" is a script.
head: error reading ‘ztj’: Is a directory
File "ztj" is not a script.
File "ztj.sh" is a script.
File "ztj.txt" is not a script.
[root@logstash ~]#